MySQL select specific entry from a table which is not in another table

后端 未结 3 680
旧巷少年郎
旧巷少年郎 2021-01-21 12:15

I have a problem in MySQL when trying to select specific entry from table which is not in another table. I know this sentence sounds nuts but here is an example what I am trying

相关标签:
3条回答
  • 2021-01-21 12:43

    Try this:

    SELECT user_id
    FROM users
    WHERE user_id NOT IN (
      SELECT user_id
      FROM views
      WHERE article_id = 10
    )
    
    0 讨论(0)
  • 2021-01-21 12:52
    SELECT user_id FROM users u
    LEFT JOIN views v ON v.user_id=u.user_id AND v.article_id = 10
    WHERE v.user_id IS NULL
    
    0 讨论(0)
  • 2021-01-21 13:05

    doing NOT IN queries are not typically great for performance... instead, using LEFT JOIN and looking for NULL would be better.

    select 
          u.User_ID
       from 
          Users U
             LEFT JOIN Views V
                on U.User_ID = V.User_ID 
               AND v.Article_ID = 10
       where
          V.User_ID IS NULL
    
    0 讨论(0)
提交回复
热议问题