sql - Postgres Join Query is SOMETIMES taking the cartesian product -
i'm attempting join multiple tables 1 query , getting inconsistent results database, believe query taking cartesian product of users, when want users in directconversation.
the schema reference:
the query (where $id stands variable user.id):
select c.*, count(dm.id), u1.first_name, u1.last_name, u1.company, u1.picture, u2.first_name, u2.last_name, u2.company, u2.picture "directconversation" c, "directmessage" dm, "profile" u1, "profile" u2 u1."id_user" = c."id_user1" , u2."id_user" = c."id_user2" , c.id = dm."id_directconversation" , dm.viewed = 'f' , dm.deleted = 'f' , c."id_user1" = $id or c."id_user2" = $id group c.id, u1.id, u2.id;
the expected result (the result when user id = 1 ):
id | id_user1 | id_user2 | count | first_name | last_name | company | picture | first_name | last_name | company | picture ----+----------+----------+-------+------------+-----------+--------------------+-----------------------------------------------------------------------------+------------+-----------+----------------+-------------------------------------------------------------------------- 1 | 1 | 2 | 3 | albert | einstein | alberts inventions | http://upload.wikimedia.org/wikipedia/commons/d/d3/albert_einstein_head.jpg | nikola | tesla | teslas widgets | http://upload.wikimedia.org/wikipedia/commons/7/79/tesla_circa_1890.jpeg (1 row) (end)
the error result (the result when user id= 2):
id | id_user1 | id_user2 | count | first_name | last_name | company | picture | first_name | last_name | company | picture ----+----------+----------+-------+------------+-----------+--------------------+----------------------------------------------------------------------------------------------------------------+------------+-----------+--------------------+---------------------------------------------------------------------------------------------------------------- 1 | 1 | 2 | 4 | albert | einstein | alberts inventions | http://upload.wikimedia.org/wikipedia/commons/d/d3/albert_einstein_head.jpg | albert | einstein | alberts inventions | http://upload.wikimedia.org/wikipedia/commons/d/d3/albert_einstein_head.jpg 1 | 1 | 2 | 4 | albert | einstein | alberts inventions | http://upload.wikimedia.org/wikipedia/commons/d/d3/albert_einstein_head.jpg | nikola | tesla | teslas widgets | http://upload.wikimedia.org/wikipedia/commons/7/79/tesla_circa_1890.jpeg 1 | 1 | 2 | 4 | albert | einstein | alberts inventions | http://upload.wikimedia.org/wikipedia/commons/d/d3/albert_einstein_head.jpg | rosalind | franklin | dna r | http://upload.wikimedia.org/wikipedia/en/9/97/rosalind_franklin.jpg 1 | 1 | 2 | 4 | albert | einstein | alberts inventions | http://upload.wikimedia.org/wikipedia/commons/d/d3/albert_einstein_head.jpg | charles | babbage | babbages cabbages | http://upload.wikimedia.org/wikipedia/commons/6/6b/charles_babbage_-_1860.jpg
... note truncated brevity. believe taking cartesian product of users, unaware why
the version of postgres i'm using:
version
----------------------------------------------------------------------------------------------------- postgresql 9.3.6 on x86_64-unknown-linux-gnu, compiled gcc (ubuntu 4.8.2-19ubuntu1) 4.8.2, 64-bit
i moved clauses joins on statement , made proper joins, if doesn't work i'll set sqlfiddle , see problem sql is
select c.*, count(dm.id), u1.first_name, u1.last_name, u1.company, u1.picture, u2.first_name, u2.last_name, u2.company, u2.picture "directconversation" c join "directmessage" dm on c.id = dm."id_directconversation" join "profile" u1 on u1."id_user" = c."id_user1" join "profile" u2 on u2."id_user" = c."id_user2" dm.viewed = 'f' , dm.deleted = 'f' , (c."id_user1" = $id or c."id_user2" = $id) group c.id, u1.id, u2.id;
edit: grouped or clause safe
Comments
Post a Comment