0

I have a query that selects all columns from multiple tables, but it's returning multiples of the same values (I only want distinct values).

How can I incorporate something like this? When I try this, it still

Select Distinct A.*, B.*, C.*....

Does distinct only work when selecting the column names and not all (*) ? In this reference it says distinct in reference to column names, not across all of the tables. Is there any way that I can do this?

edit - I added more info below

Sorry guys, I just got back onto my computer. Also, I just realized that my query itself is the issue, and Distinct has nothing to do with it.

So, the overall goal of my Query is to do the following

  1. Generate a list of friends that a user has
  2. Go through the friends and check their activities (posting, adding friends, etc..)
  3. Display a list of friends and their activities sorted by date (I guess like a facebook wall kind of deal).

Here are my tables

update_id |  update | userid  | timestamp  //updates table

post_id |  post | userid  | timestamp      //posts table

user_1 |   user_2  | status   |  timestamp   //friends table

Here is my query

SELECT U.* , P.* ,F.* FROM posts AS P 
JOIN updates AS U ON P.userid = U.userid 
JOIN friends AS F ON P.userid = F.user_2 or F.user_1
WHERE P.userid IN (
        select user_1 from friends where user_2 = '1'
        union
        select user_2 from friends where user_1 = '1'
        union
        select userid from org_members where org_id = '1'
        union
        select org_id from org_members where userid = '1'
        ) 
ORDER BY P.timestamp, U.timestamp, F.timestamp limit 30

The issue I'm having with this (that I thought was related to distinct), is that if values are found to meet the requirements in, say table Friends, a value for the Posts table will appear too. This means when I'm displaying the output of the SQL statement, it appears as if the Posts value is shown multiple times, when the actual values I'm looking for are also displayed

The output will appear something like this (notice difference between post value in the rows)

update_id |  update | userid  | timestamp | post_id |  post | userid  | timestamp | user_1 |   user_2  | status   |  timestamp

     1    | update1 |   1    | 02/01/2013 |    1    |  post1|  1      |  2/02/2013|  1     |   2       |  1       | 01/30/2013
     1    | update1 |   1    | 02/01/2013 |    2    |  post2|  1      |  2/03/2013|  1     |   2       |  1       | 01/30/2013

So, as you can see, I thought I was having a distinct issue (because update1 appeared both times), but the query actually just selects all the values regardless. I get the results I'm looking for in the Post table, but all the other values are returned. So, when I display the table in PHP/HTML, the Post value will display, but I also get duplicates of the updates (just for this example)

4

4 回答 4

0

我想你想要这个:

select *
from tableA
union
select * 
from tableB
union
select * 
from tableC

这假设 HHS 表都具有相同数量的列并且它们具有相同的数据类型。这不是,您必须选择特定的列才能做到这一点。

于 2013-02-03T09:21:39.347 回答
0

如果您多解释一下您正在查询的表之间的连接是什么,那将很容易,因为您可以使用联接、联合(如上所述)甚至分组依据...

于 2013-02-03T15:08:19.953 回答
0

您更新后的帖子显示的 JOIN 条件之一为:

JOIN friends AS F ON P.userid = F.user_2 OR F.user_1

这相当于:

JOIN friends AS F ON (P.userid = F.user_2 OR F.user_1  != 0)

并将包含许多您不打算包含的行。您可能打算:

JOIN friends AS F ON (P.userid = F.user_2 OR P.userid = F.user_1)
于 2013-02-03T22:10:06.200 回答
0

当您选择 distinct * 时,您选择每一行,包括使记录唯一的那一行。如果你想要比你得到的更好的东西,你必须在你的选择子句中输入各个列的名称。

于 2013-02-02T21:50:00.507 回答