Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aZ6sw-0000pX-4k for pgsql-sql@arkaria.postgresql.org; Fri, 26 Feb 2016 01:07:58 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aZ6sv-0000G9-IJ for pgsql-sql@arkaria.postgresql.org; Fri, 26 Feb 2016 01:07:57 +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 1aZ6rw-0007ad-V4 for pgsql-sql@postgresql.org; Fri, 26 Feb 2016 01:06:57 +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 1aZ6rt-0004Y3-Fv for pgsql-sql@postgresql.org; Fri, 26 Feb 2016 01:06:55 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id D9A9F207B1 for ; Thu, 25 Feb 2016 20:06:52 -0500 (EST) Received: from frontend2 ([10.202.2.161]) by compute4.internal (MEProxy); Thu, 25 Feb 2016 20:06:52 -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=gVbIiCCFr6KGfBn8CqDYoZLQtz0=; b=d0jlEB GhZdxoT5wnYGvLXY3HI+sGEB4PJWjdLfG0C5NZoZn99IaCL3fTqkjSMTOkHBbBAr cYkxA0tYVMYY9Ns7ZE7+H1h+c8EGLaiuOL8Img7HPahLqY1v/PyhqQM9CuWitSAN 5JG1vCI1In1xwFeO/8Ojm2Oy5YapScrPyEyAU= 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=gVbIiCCFr6KGfBn 8CqDYoZLQtz0=; b=e9FAxTcI70l6OwshVKSUNctZiC33GwlJTlV3ECz5g/0JAFk 0TuD2HbP9nYD8pD45r+M3tSeeXH7tnCLsz7QpVKZjwVXH8FAAj9eqXB3bZV0eX7Z UT13eCsgH+5CNraPJHcfTwLS+GhYwWIhdYd1b1pwHOhX22pn8knxUVH1iPV8= X-Sasl-enc: GKHhed4sicO7RPsPlW4QDlqymwg68F2RT1klqvvXGv1b 1456448812 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 5C3B668019E; Thu, 25 Feb 2016 20:06:52 -0500 (EST) Subject: Re: Query about foreign key details for php framework To: David Binney , pgsql-sql@postgresql.org References: <56CFA136.4000508@aklaver.com> From: Adrian Klaver Message-ID: <56CFA4E1.8040204@aklaver.com> Date: Thu, 25 Feb 2016 17:05:37 -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: 7bit 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:56 PM, David Binney wrote: > Hey Adrian, > > 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 > > Those are the needed columns, as the end resultset. But that is different then what you are asking from the MySQL query: http://dev.mysql.com/doc/refman/5.7/en/information-schema.html There is no constraint_type in the information_schema tables you reference in the MySQL query, unless I am missing something. > > On Fri, 26 Feb 2016 at 10:51 Adrian Klaver > wrote: > > 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 > > -- > 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