如何在一个MySQL查询中选择2个联接表?

时间:2019-02-07 14:40:01

标签: php mysql sql

我有1个master_table和2个sub_tables。我想将3列连接在一起(但是问题是2个sub_tables没有共享相同值的任何列),然后根据来自2个sub_tables的2个不同的列进行SELECT *。

我已经搜索并尝试了许多编码方式,但是找不到解决方案。

SELECT *
FROM (master INNER JOIN sub_1 ON master.id=sub_1.id WHERE sub_1.column_1 = 'Y')
AND (master INNER JOIN sub_2 ON master.id=sub_2.id WHERE sub_2.column_2 = 'Y')
ORDER BY master.id

++++++++++++++++++++++++++++++++++++++++++++++++ +++
*最后,解决了。请参阅本文底部的解决方案。 * ++++++++++++++++++++++++++++++++++++++++++++++++++++

===========

编辑:详细说明我的数据,问题和MySQL代码

我有3张表存储在MySQL中

Master_table: regist
------------------------------------------
| reg_no | firstname | lastname | submit |
------------------------------------------
|   1    |  first_A  |  last_A  |   N    |
|   2    |  first_B  |  last_B  |   A    |
|   3    |  first_C  |  last_C  |   P    |
|   4    |  first_D  |  last_D  |   P    |
|   5    |  first_E  |  last_E  |   A    |
|   6    |  first_F  |  last_F  |   N    |
|   7    |  first_G  |  last_G  |   N    |
|   8    |  first_H  |  last_H  |   A    |
------------------------------------------

Sub_1: sub_A                       Sub_2: sub_P
------------------------------        ------------------------------
| reg_no | A_title | reply_A |        | reg_no | P_title | reply_P |
------------------------------        ------------------------------
|   2    |   222   |    Y    |        |   3    |   333   |    N    |
|   5    |   555   |    N    |        |   4    |   444   |    Y    |
|   8    |   888   |    Y    |        ------------------------------
------------------------------

我想创建一个查询,给出这样的结果

----------------------------------------------------------------------------------
| reg_no | firstname | lastname | submit | A_title | reply_A | P_title | reply_P |
----------------------------------------------------------------------------------
|   2    |  first_B  |  last_B  |   A    |   222   |    Y    |         |         |
|   8    |  first_H  |  last_H  |   A    |   888   |    Y    |         |         |
|   4    |  first_D  |  last_D  |   P    |         |         |   444   |    Y    |
----------------------------------------------------------------------------------

or

-----------------------------------------------------------
| reg_no | firstname | lastname | submit | title |  reply |
-----------------------------------------------------------
|   2    |  first_B  |  last_B  |   A    |  222  |   Y    |
|   8    |  first_H  |  last_H  |   A    |  888  |   Y    |
|   4    |  first_D  |  last_D  |   P    |  444  |   Y    |
-----------------------------------------------------------

$sql = "SELECT *
    FROM (regist INNER JOIN sub_A ON regist.reg_no = sub_A.reg_no WHERE sub_A.reply_A = 'Y')
    AND  (regist INNER JOIN sub_P ON regist.reg_no = sub_P.reg_no WHERE sub_P.reply_P = 'Y')
    ORDER BY regist.reg_no";

预期结果: 回信为“ Y”的所有注册人的ECHO个人数据

if($row['submit']=="A") $title = $row['A_title'];
elseif($row['submit']=="P") $title = $row['P_title'];

$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
  echo $row['reg_no']." / ".$row['firstname']." ".$row['lastname']." / ".$title."<br>";
}

问题:我的SELECT代码导致错误。 @GMB和@Rogue的代码没有出错,但是echo没有任何作用。

如果无法根据需要编写查询代码,则只需将列名(sub_1.reply_A和sub_2.reply_P)修改为相同,然后更改其他网页中的输入代码即可。但是,最好是有办法,因为我不知道其他地方是否使用了“答复”列。

=======================

解决方案:对@Rogue代码进行一些修改

SELECT *
FROM master
LEFT OUTER JOIN sub_1
   ON master.id=sub_1.id
LEFT OUTER JOIN sub_2
   ON master.id=sub_2.id
WHERE sub_1.column_1 = 'Y'
   OR sub_2.column_2 = 'Y'
ORDER BY master.id

2 个答案:

答案 0 :(得分:3)

您是否只希望在这3个表之间使用简单的JOIN

SELECT m.*, s1.*, s2.*
FROM master m
INNER JOIN sub_1 s1 ON m.id=s1.id AND s1.column_1 = 'Y'
INNER JOIN sub_2 s2 ON m.id=s2.id AND s2.column_2 = 'Y'
ORDER BY m.id;

如果两个子表中可能都没有主记录,则可以切换到LEFT JOIN以避免将它们过滤掉。

指南:

  • 典型语法为SELECT ... FROM table1 INNER JOIN table2 ON ... INNER JOIN table3 ON...
  • 最好将与JOINed表相关的所有条件放在连接的ON子句中,而不要放在WHERE子句中
  • 避免使用SELECT *:具体要选择的列
  • 使用表别名使查询更易于阅读

答案 1 :(得分:1)

您在语法上有点不满意:

SELECT *
FROM master
LEFT OUTER JOIN sub_1
   ON master.id=sub_1.id
LEFT OUTER JOIN sub_2
   ON master.id=sub_2.id
WHERE sub_1.column_1 = 'Y'
   AND sub_2.column_2 = 'Y'
ORDER BY master.id

我个人建议不要使用SELECT *,而只使用所需的数据。至于确定使用哪种联接,我喜欢在这些时间链接到CodingHorror's blog post

编辑:根据OP的更新,将INNER换成LEFT OUTER