[英]Checking certain row from a table which is referenced to another table?
I know that the question will not make any sense at all to what I am asking, but what I mean is that..... To make it more clear, I will illustrate my problem in examples: 我知道这个问题根本不会对我的要求有任何意义,但我的意思是.....为了使其更清楚,我将在例子中说明我的问题:
I have a userpost
table that contains posts from different users. 我有一个
userpost
表,其中包含来自不同用户的帖子。
userpost table: userpost表:
+---------+--------+--------------+
| postId | userId | postMessage |
+---------+--------+--------------+
| 1 | 3 | someText |
| 2 | 5 | someText |
| 3 | 2 | sometext |
| 5 | 6 | someText |
+---------+--------+--------------+
( !REMEMBER that userId
is being referenced to users
table) ( !记住
userId
被引用到 users
表)
I have another table called favorites
where postId
is being referenced from userpost
table: 我有另一个名为
favorites
表,其中postId
是从userpost
表引用的:
favorites table: 收藏表:
+---------+--------+
| postId | userId |
+---------+--------+
| 1 | 5 |
| 3 | 2 |
+---------+--------+
What I want is to grab all the data from userpost
and check if a certain userpost has been Favorited by ( WHERE userId = 5
) let's say 我想要的是从
userpost
获取所有数据并检查某个用户帖是否已被收藏( WHERE userId = 5
)让我们说
I tried using this query but that's not what I want! 我尝试使用此查询,但这不是我想要的!
SELECT *,
(SELECT EXISTS( SELECT * FROM `favorites` INNER JOIN `userpost` on
favourites.postId = userpost.postId WHERE favorites.postId = 1
AND favorites.userId = 5)) AS isFavourited FROM userpost;
This is the result of the following query: 这是以下查询的结果:
+---------+--------+-------------+--------------+
| postId | userId | postMessage | isFavourited |
+---------+--------+-------------+--------------+
| 1 | 3 | someText | 1 |
| 2 | 5 | someText | 1 |
| 3 | 2 | someText | 1 |
| 5 | 3 | someText | 1 |
+---------+--------+-------------+--------------+
I know that I am making the mistake inside the query by using: 我知道我在查询中犯了错误:
(WHERE favorites.postId = 1 AND favorites.userId = 5)
which does return true. 这确实是真的。
I will give you an example of what I want : 我会给你一个我想要的例子:
Lets say (userId = 5)
wants to grab all the userpost
and we must get the result below: 让我们说
(userId = 5)
想要获取所有用户userpost
,我们必须得到以下结果:
+---------+--------+-------------+--------------+
| postId | userId | postMessage | isFavourited |
+---------+--------+-------------+--------------+
| 1 | 3 | someText | 1 |
| 2 | 5 | someText | 0 |
| 3 | 2 | someText | 0 |
| 5 | 3 | someText | 0 |
+---------+--------+-------------+--------------+
I think you can do something like this if I understand what you're asking : 如果我理解你的要求,我想你可以做这样的事情:
select up.*, case when f.postId is null then "0" else "1" end as isFavourited
from userpost up
left join favourites f on f.postId = up.postId and f.userId = up.userId
Try: 尝试:
SELECT *,
postId IN
(SELECT postId FROM favourites WHERE userId = 5)
AS isFavourited
FROM userPost
Your query checks that a row exists where user 5 favourites post 1; 您的查询检查是否存在用户5收藏夹发布1的行; not where user 5 favorites the post being selected in that row of the return.
而不是用户5最喜欢在该行返回中选择的帖子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.