给三个表之间的匹配SQL [英] Give the Match between Three Tables SQL

查看:128
本文介绍了给三个表之间的匹配SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本,以检查新闻是否被多个用户看到为了检查那个用户从未检查过的新消息。我认为一个服务不断要求服务器给予用户从未读过的新闻。但我不知道如何写它(我使用笛卡尔产品,但没有工作)。
换句话说,如何检查用户是否没有阅读这个消息,并给我一种方式提供通知。
如果有人可以帮助我请?
这些是我的数据库中的三个表:用户,新闻和查看。

I am writing a script to check if the news are seen or not by multiple users In order to check new news that never checked by that user. I think that a service keep asking the server to give the News that never been read by the User. But i didn't know how to write about it.( I've used the Cartesian product but that didn't work). In other Words, how to check if the user didn't read this news and give me a way to provide a notification. If anyone can help me Please? These are thethree tables in my database:Users,News,and Seen.

这是Users表:

User_ID | User_Name
--------------------
1       | John
2       | Carl
3       | Tomas
4       | Adam
5       | Nancy

和新闻表:

News_ID | News_Text
--------------------
1       | Hello World
2       | This is My car
3       | I had Ate pizza
4       | No Body Want Programming
5       | C++ Programming

这也是Seen表:

ID   | User_Id  | News_Id
---------------------------
1    |  1       | 2
2    |  1       | 3
3    |  4       | 1
4    |  5       | 3
5    |  1       | 4


推荐答案

SELECT News.News_ID AS 'Unseen News ID'
FROM News 
    LEFT JOIN Seen ON Seen.News_ID = News.News_ID AND Seen.User_ID = <UserIdYouCareAbout>
WHERE
    Seen.ID IS NULL

这篇关于给三个表之间的匹配SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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