mysql - 数据库中的多重连接搜索

标签 mysql sql select join

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

我有 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`;

最佳答案

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

 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';

关于mysql - 数据库中的多重连接搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29748297/

相关文章:

mysql - SQL 计数结果并加入

mysql - 从数据库中选择一列中的值不同的行,并限制为最新的 5 行

MySQL 随机选择每组 1 个图像 1 个 SELECT 语句

mysql - 元表 VS 字段多、规模大的表。性能方面

sql - 查询如何在sql中添加缺失的日期

javascript - 根据先前选择标签中的选择设置选择标签的可见性

javascript - 使用复杂对象上的挖空功能手动设置选择框的默认值

mysql - 实时服务器中的 mysql 匹配不起作用

找不到行时 MYSQL 左连接不工作

sql - 修复 Amazon Redshift 上计算 DAU 和 MAU 时的 MAU 问题