mysql查询用于从不同表中获取相关数据以用于建议的连接
我们正在开发具有不同概念的类似社交网站的门户...但是为了进行建议的连接,我们在 mysql 查询中遇到了困难... 我们正在尝试为用户提供类似的数据...
SELECT u.*
FROM educonnect_user u
LEFT OUTER JOIN educonnect_user_qualification q ON u.id = q.user_id
LEFT OUTER JOIN educonnect_user_contact a1 ON u.id = a1.user_id
WHERE q.type_of_institution in
(
SELECT type_of_institution
FROM educonnect_user_qualification qi
WHERE qi.user_id = 3
)
AND q.college in
(
select college
from educonnect_user_qualification qc
where qc.user_id = 3
)
AND q.country in
(
select country
from educonnect_user_qualification qco
where qco.user_id = 3
)
AND a1.country in
(
select country
from educonnect_user_contact cc
where cc.user_id = 3
)
AND a1.state in
(
select state
from educonnect_user_contact cs
where cs.user_id = 3
)
像这样我要加入 10 个表..但问题是无论我在哪里给出 AND
运算符都不会生成结果,如果我给出 OR 运算符它返回所有用户..这些运算符将给出我知道的输出的逻辑..但是对于这个问题,我需要有效工作的不同建议..否则可以使用任何其他特定运算符更改查询? ?
we are developing portal like social network site with different concept...but for doing suggested connections we got strucked in mysql queries...
we are trying to take users with similat data ...
SELECT u.*
FROM educonnect_user u
LEFT OUTER JOIN educonnect_user_qualification q ON u.id = q.user_id
LEFT OUTER JOIN educonnect_user_contact a1 ON u.id = a1.user_id
WHERE q.type_of_institution in
(
SELECT type_of_institution
FROM educonnect_user_qualification qi
WHERE qi.user_id = 3
)
AND q.college in
(
select college
from educonnect_user_qualification qc
where qc.user_id = 3
)
AND q.country in
(
select country
from educonnect_user_qualification qco
where qco.user_id = 3
)
AND a1.country in
(
select country
from educonnect_user_contact cc
where cc.user_id = 3
)
AND a1.state in
(
select state
from educonnect_user_contact cs
where cs.user_id = 3
)
Like this I am joining 10 tables ..but the problem is wherever i gave AND
operator no result generated and if i gave OR
operator it returns all users ..its the logic these operator will give output which I know..but for this problem i need different suggestions which would work effectively..or else the query can be changed with any other specific operator???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要所有这些
WHERE IN
条件。您只需要where educonnect_user.user_id = 3
,因为educonnect_user
表与其他两个表educonnect_user_qualification
和educonnect_user_contact
连接>,因此连接将确保对于具有id=3
的用户,字段:type_of_institution
、college
、country
,..(和其他字段)存在于同一用户的其他两个表中,但是您需要关心您需要什么类型的联接,具体取决于您想要从您的数据库中选择哪些字段表格是否左
、右
。所以我认为以下查询您正在寻找什么:You didn't need all these
WHERE IN
conditions. You just needwhere educonnect_user.user_id = 3
since theeduconnect_user
table is joined with the other two tableseduconnect_user_qualification
andeduconnect_user_contact
, therefore the join will insure that for the user withid=3
the fields:type_of_institution
,college
,country
,..( and other fields ) are exists in the two other tables for the same user, But you need to care about what type of join you need depending on what fields you want to select from your tables whetherLeft
,right
. So I think the following query what you are looking for:希望这会有所帮助::
Hope this will help ::