mysql查询用于从不同表中获取相关数据以用于建议的连接

发布于 2024-12-25 22:25:11 字数 1290 浏览 0 评论 0原文

我们正在开发具有不同概念的类似社交网站的门户...但是为了进行建议的连接,我们在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦途 2025-01-01 22:25:11

您不需要所有这些 WHERE IN 条件。您只需要 where educonnect_user.user_id = 3,因为 educonnect_user 表与其他两个表 educonnect_user_qualificationeduconnect_user_contact 连接>,因此连接将确保对于具有 id=3 的用户,字段:type_of_institutioncollegecountry,..(和其他字段)存在于同一用户的其他两个表中,但是您需要关心您需要什么类型的联接,具体取决于您想要从您的数据库中选择哪些字段表格是否。所以我认为以下查询您正在寻找什么:

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 u.user_id = 3

You didn't need all these WHERE IN conditions. You just need where educonnect_user.user_id = 3 since the educonnect_user table is joined with the other two tables educonnect_user_qualification and educonnect_user_contact, therefore the join will insure that for the user with id=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 whether Left, right. So I think the following query what you are looking for:

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 u.user_id = 3
没有你我更好 2025-01-01 22:25:11

希望这会有所帮助::

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 
    left join educonnect_user_qualification qi on (q.type_of_institution=qi.type_of_institution and qi.user_id=3)
  left join educonnect_user_qualification qc  on (q.college=qc.college and qc.user_id=3)
  left join educonnect_user_qualification qco on (q.country=qco.country and qco.user_id=3)
  left join educonnect_user_contact cc on (a1.country=cc.country and cc.user_id=3)
  left join educonnect_user_contact cs on (a1.state=cs.state and cs.user_id=3)  

Hope this will help ::

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 
    left join educonnect_user_qualification qi on (q.type_of_institution=qi.type_of_institution and qi.user_id=3)
  left join educonnect_user_qualification qc  on (q.college=qc.college and qc.user_id=3)
  left join educonnect_user_qualification qco on (q.country=qco.country and qco.user_id=3)
  left join educonnect_user_contact cc on (a1.country=cc.country and cc.user_id=3)
  left join educonnect_user_contact cs on (a1.state=cs.state and cs.user_id=3)  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文