【问题标题】:Multiple Join Searching in Database数据库中的多连接搜索
【发布时间】:2015-06-27 04:52:03
【问题描述】:

我想在我正在进行的项目中从数据库中获取记录。

我有 5 张桌子;每个人都制作保存在起始四个表中的配置文件,然后定义合作伙伴配置文件。我想获取所有符合个人资料的合作伙伴名称。

这是我的表格结构:

Basic Info (reg_id, name, gender, dob, martial_status)
Education (id,reg_id,education,college)
Location (id,reg_id,country,state, city)
Other_details (id,reg_id,height, weight)
Partner (id, reg_id, gender, dob, education, college, country, state, city, height, weight,martial_status) [ This is the Main Table]..

到目前为止,我尝试了这个但没有运气:

SELECT `basic_info`.`reg_id`,
       `basic_information`.`dob`,
       `other_detail`.`height`,
       `location`.`city`,
       `education`.`education` 
FROM `basic_information`
INNER JOIN(
          SELECT * 
          FROM `patner` 
          WHERE `patner`.`reg_id`='shi01') `patner` 
      ON `basic_information`.`martial_status`=`partner`.`martial_status` 
      AND `basic_information`.`reg_id`!='shi01'
INNER JOIN `education` 
      ON `patner`.`education`=`education`.`education` 
      AND `patner`.`education`=`education`.`education`
INNER JOIN `location` 
      ON `patner`.`city`=`location`.`city`
INNER JOIN `other_detail` 
      ON `patner`.`bodytype`=`other_detail`.`body_type` 
      AND `patner`.`skin`=`other_detail`.`skin` 
      AND `patner`.`height1` <=  `other_detail`.`height` 
GROUP BY `basic_information`.`reg_id`;

【问题讨论】:

  • 请提供表格定义。表中的主键和外键是什么。 ?
  • 在第一个表中 Reg_id 是 PK.. 在 Rest 中是 FK..

标签: mysql sql select join


【解决方案1】:

您在编写连接条件时犯了一些错误。我试图修复它。试试这个,应该可以的;

 SELECT BI.reg_id, BI.name
 FROM basic_info BI
 INNER JOIN
     Education E ON BI.reg_id = E.reg_id
 INNER JOIN
     Location L ON BI.reg_id = L.reg_id
 INNER JOIN
     Other_details OD ON BI.reg_id = OD.reg_id
 INNER JOIN
     (Select reg_id, martial_status, height, education, city FROM Partner where reg_id = 'Your_ID') P 
     ON BI.reg_id = P.reg_id
 WHERE
    BI.marital_status = P.marital_Status
    AND E.Education = P.Education
    AND L.city = P.City
    AND OD.height <= P.Height
    AND BI.reg_id <> 'Your_ID';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 2012-03-14
    • 1970-01-01
    • 2019-02-26
    • 1970-01-01
    • 2019-09-13
    相关资源
    最近更新 更多