【问题标题】:mysql select distinct where clausemysql select distinct where 子句
【发布时间】:2010-11-17 15:36:19
【问题描述】:

我有这张表,我想选择同时存在 event='AAAAAAAAAA' 和 event ='XXXXXXXXXX' 的所有不同 user_id 值(即只有结果应该是 user_id=123456789)

unique_row_id  user_id          event       event_timestamp
----------------------------------------------------------------
0              123456789        AAAAAAAAAA  2010-01-20 15:00:00
1              123456789        abcdefghij  2010-01-20 15:00:05
2              123456789        XXXXXXXXXX  2010-01-20 15:00:15
3              987654321        AAAAAAAAAA  2010-01-20 16:00:00
4              987654321        abcdefghij  2010-01-20 16:00:05
5              987654321        abcdefghij  2010-01-20 16:00:15
6              111111111        XXXXXXXXXX  2010-01-20 16:01:00
7              111111111        XXXXXXXXXX  2010-01-20 16:01:05
8              111111111        XXXXXXXXXX  2010-01-20 16:01:15

我都试过了:

SELECT distinct user_id from mydata where event='AAAAAAAAAA' and event='XXXXXXXXXX'

这给了我一个空的结果集,并且

SELECT distinct user_id from mydata where event='AAAAAAAAAA' or event='XXXXXXXXXX'

这给了我一切。有什么建议吗?我猜我需要走“加入”这条路吗?

【问题讨论】:

    标签: mysql join


    【解决方案1】:

    试试这个:

    select user_id from mydata where event='AAAAAAAAAAA'
    union
    select user_id from mydata where event='XXXXXXXXXXX';
    

    这将选择两个列表(用户 w/ AAA,用户 w/ XXX),然后将它们合并在一起,删除重复的行。

    希望有帮助!

    谢谢, 乔

    【讨论】:

    • 比我写的 SELECT ... WHERE EXISTS (...) 查询要好得多。 ;)
    • 实际上,这是错误的,因为它是“两者”而不是“两者”。现在修改:)
    • select distinct user_id from(select user_id from mydata where event='AAAAAAAAAAAA' union select user_id from mydata where event='XXXXXXXXXXXX';)
    • 但是 UNION 有两个以上的事件怎么样。
    【解决方案2】:

    我会用子查询来做:

    select distinct user_id
    from mydata m
    where event = 'AAAAAAAAAA'
    and exists (
      select 1
      from mydata
      where user_id = m.user_id
      and event = 'XXXXXXXXXX'
    );
    

    【讨论】:

      【解决方案3】:

      WHERE 子句的条件一次只适用于一行。 为了检查两行,您必须使用 JOIN:

      SELECT distinct a.user_id 
      FROM mydata a, mydata b 
      WHERE a.event='AAAAAAAAAA' AND b.event='XXXXXXXXXX' AND a.user_id = b.user_id
      

      如果您知道一个事件每个用户只能出现一次,您可以使用 GROUP BY:

      SELECT user_id
      FROM mydata 
      WHERE event in ('AAAAAAAAAA', 'XXXXXXXXXX')
      GROUP BY user_id
      HAVING count(user_id)=2
      

      【讨论】:

        【解决方案4】:
        SELECT
            DISTINCT user_id
        FROM
            my_table AS outer_table
        WHERE
            EXISTS (
                SELECT
                    1
                FROM
                    my_table AS inner_table_A
                WHERE
                    inner_table_A.user_id = outer_table.user_id AND
                    inner_table_A.event = 'AAAAAAAAAA'
            ) AND
            EXISTS (
                SELECT
                    1
                FROM
                    my_table AS inner_table_B
                WHERE
                    inner_table_B.user_id = outer_table.user_id AND
                    inner_table_B.event = 'XXXXXXXXXX'
            )
        

        【讨论】:

          【解决方案5】:

          我会使用这样的东西......

          SET @events := 'AAAAAAAAAA,XXXXXXXXXX';
          
          SELECT user_id 
          FROM mydata
          GROUP BY user_id
          HAVING GROUP_CONCAT(DISTINCT event ORDER BY event SEPARATOR ',') = @events;
          

          【讨论】:

            猜你喜欢
            • 2013-05-27
            • 2010-12-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-02-28
            • 1970-01-01
            • 2011-08-02
            • 1970-01-01
            相关资源
            最近更新 更多