Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aZ6cp-0008PB-7A for pgsql-sql@arkaria.postgresql.org; Fri, 26 Feb 2016 00:51:19 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aZ6co-0002ic-Df for pgsql-sql@arkaria.postgresql.org; Fri, 26 Feb 2016 00:51:18 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aZ6cm-0002gu-JR for pgsql-sql@postgresql.org; Fri, 26 Feb 2016 00:51:16 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aZ6ck-0004EZ-8B for pgsql-sql@postgresql.org; Fri, 26 Feb 2016 00:51:15 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 9031020989 for ; Thu, 25 Feb 2016 19:51:13 -0500 (EST) Received: from frontend1 ([10.202.2.160]) by compute4.internal (MEProxy); Thu, 25 Feb 2016 19:51:13 -0500 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=+teINIL0vezZf9jlF1LyzL6jAEY=; b=i4Pfs1 a6F8vq+2pOWiFVf9x1dmuGi5r9P+Xqln9g0uZ5cMhIOvJxqZjYayOAOEzAYox9i5 7p/MWvmXGAvGm3vOHpGHKRhgh5w7QpFuQL9EE/bmY2lJTFWUGYTSNoV3NVSiRIGW 8V1mcV9j039houTXxBp5jrmOS30FXjomtLrjY= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=+teINIL0vezZf9j lF1LyzL6jAEY=; b=GryWtpX9jwoaD8UzcMZHpbJ/wL0X8mWj/5MS6c/IH7ado24 qBGSm0jcHmBnOKLUUgfXiV8V/geyIrpXbUJ4q4E9dHZLJmuX8781arZ4eQJpj3dv OWtr31oGRSc51zBwAVvAD0OTR1+Ux9dn+PS7CIWnwQh588f/8M6aZDTcifig= X-Sasl-enc: 5IFJE436wq8CjuFZ86PWhTAGMLoRc6pQbCaoRAMC0t+C 1456447873 Received: from [192.168.1.2] (174-21-70-246.tukw.qwest.net [174.21.70.246]) by mail.messagingengine.com (Postfix) with ESMTPA id 160D7C00014; Thu, 25 Feb 2016 19:51:13 -0500 (EST) Subject: Re: Query about foreign key details for php framework To: David Binney , pgsql-sql@postgresql.org References: From: Adrian Klaver Message-ID: <56CFA136.4000508@aklaver.com> Date: Thu, 25 Feb 2016 16:49:58 -0800 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Pg-Spam-Score: -2.0 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org On 02/25/2016 04:38 PM, David Binney wrote: > Hey guys, > > I am having a tricky problem which I have not needed to solve before. > Basically one of the php frameworks I am using needs to get the same > dataset from mysql and postgres but I am not sure how to do the joins. > > Below i have the mysql version of the query which work ok, and after > that i have my attempt at the postgresql version, which is not joined > correctly. Any help would be greatly appreciated, and in the meantime i > will keep guessing which columns need to be joined for those three > tables, but I am thinking there could be a view or something to solve my > problem straight away?? The * in your MySQL query hides what it is you are trying retrieve. So what information are you after? Or to put it another way, what fails in the Postgres version? > > -------mysql working version---------- > SELECT >   * > FROM >   information_schema.key_column_usage AS kcu >   INNER JOIN information_schema.referential_constraints AS rc ON ( >     kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME >     AND kcu.CONSTRAINT_SCHEMA = rc.CONSTRAINT_SCHEMA >   ) > WHERE >   kcu.TABLE_SCHEMA = 'timetable' >   AND kcu.TABLE_NAME = 'issues' >   AND rc.TABLE_NAME = 'issues' > > ---- postgresql partial working version-------------- > > select > rc.constraint_name AS name, > tc.constraint_type AS type, > kcu.column_name, > rc.match_option AS match_type, > rc.update_rule AS on_update, > rc.delete_rule AS on_delete, > kcu.table_name AS references_table, > kcu.column_name AS references_field, > kcu.ordinal_position > FROM > (select distinct * from information_schema.referential_constraints) rc > JOIN information_schema.key_column_usage kcu > ON kcu.constraint_name = rc.constraint_name > AND kcu.constraint_schema = rc.constraint_schema > JOIN information_schema.table_constraints tc ON tc.constraint_name = > rc.constraint_name > AND tc.constraint_schema = rc.constraint_schema > AND tc.constraint_name = rc.constraint_name > AND tc.table_schema = rc.constraint_schema > WHERE > kcu.table_name = 'issues' > AND rc.constraint_schema = 'public' > AND tc.constraint_type = 'FOREIGN KEY' > ORDER BY > rc.constraint_name, > cu.ordinal_position; > > -- > Cheers David Binney -- Adrian Klaver adrian.klaver@aklaver.com -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql