[英]MYSQL Inner Joins with OR condition
I have 3 tables with the following columns: 我有3个表格,其中包含以下各列:
Table1: 表格1:
userId | seriesId | content
Table2: 表2:
followerId | followeeId
Table3: 表3:
followerId | seriesId
What I'm trying to do is something like this: 我想做的是这样的:
SELECT Table1.content
FROM Table1
(INNER JOIN Table2 ON Table2.followeeId=Table1.userId WHERE Table2.followerId=X)
OR (INNER JOIN Table3 ON Table3.seriesId=Table1.seriesId WHERE Table3.followerId=X)
'X' will be some dynamically generated number. “ X”将是一些动态生成的数字。 I'm not sure if this is even the right approach but you get what I'm trying to do right?
我不确定这是否是正确的方法,但是您知道我在尝试正确吗? My question is, what is the right way to implement something like this?
我的问题是,实现这种目标的正确方法是什么? Please let me know in the comments if any part of my question is not clear.
如果我的问题的任何部分不清楚,请在评论中让我知道。
Use a UNION
: 使用
UNION
:
SELECT Table1.content
FROM Table1
INNER JOIN Table2 ON Table2.followeeId=Table1.userId
WHERE Table2.followerId=X
UNION
SELECT Table1.content
INNER JOIN Table3 ON Table3.seriesId=Table1.seriesId
WHERE Table3.followerId=X
You can do this too.. 你也可以这样做..
SELECT Table1.content
FROM Table1
LEFT JOIN Table2 ON Table2.followeeId=Table1.userId
LEFT JOIN Table3 ON Table3.seriesId=Table1.seriesId
where Table2.followerId=X or Table3.followerId=X;
The other way is Union 另一种方式是联盟
select table1.content
from table1
INNER JOIN Table2 ON Table2.followeeId=Table1.userId
where followerId = X
union
select table1.content
from table1
INNER JOIN Table3 ON Table3.seriesId = Table1.seriesId
where Table3.followerId = X;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.