MySQL从不在另一个表中的表中选择特定条目 [英] MySQL select specific entry from a table which is not in another table

查看:86
本文介绍了MySQL从不在另一个表中的表中选择特定条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MySQL中有一个问题,当尝试从表中选择不在另一个表中的特定条目。我知道这句话听起来很难,但这里是我想要做的一个例子。



表用户:

  user_id,用户名,密码(以及对此不重要的其他列)

表文章:

  article_is,user_id,content(plus others)



表视图(用于在用户查看特定文章时存储数据):

  view_id,article_id,user_id,date 

未读取特定文章的用户,例如ID为10的文章。因此,他们在视图表中没有条目。



我希望现在更有意义。
感谢您的答案。 V。

解决方案

  SELECT user_id FROM users u 
LEFT JOIN views v.user_id = u.user_id AND v.article_id = 10
WHERE v.user_id IS NULL


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 to do.

Table users:

    user_id,username,password (plus other columns not important to this)

Table articles:

    article_is,user_id,content (plus others)

Table views (used to store data if user viewed specific article):

   view_id,article_id,user_id,date

Now, I am trying to select those users who has NOT read a specific article, for example article with id 10. So they have no entry in the views table.

I hope it makes more sense now. Thanks for your answers. V.

解决方案

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

这篇关于MySQL从不在另一个表中的表中选择特定条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆