仅加入值与变量匹配的特定行

时间:2011-10-03 01:29:14

标签: mysql select join

我有多个包含不同数量列的MySQL表。在连接了三个表之后,我得到了一个结果表,其结构如下:

+------------+------------+-----------+-------+------+
| student_id | first_name | last_name | class | rank |
+------------+------------+-----------+-------+------+
| 1          | John       | Doe       | 2012  | 1    |
+------------+------------+-----------+-------+------+
| 2          | Suzy       | Public    | 2013  | 12   |
+------------+------------+-----------+-------+------+
| 3          | Mike       | Smith     | 2014  | 50   |
+------------+------------+-----------+-------+------+

我还有两个未参与初始连接的附加表:

感兴趣

+-------------+------------+-----------------------+----------------+
| interest_id | student_id | employer_interest     | interest_level |
+-------------+------------+-----------------------+----------------+
| 1           | 1          | Wayne Enterprises     | High           |
+-------------+------------+-----------------------+----------------+
| 2           | 1          | Gotham National Bank  | Medium         |
+-------------+------------+-----------------------+----------------+
| 3           | 2          | Wayne Enterprises     | Low            |
+-------------+------------+-----------------------+----------------+
| 4           | 3          | Gotham National Bank  | High           |
+-------------+------------+-----------------------+----------------+

优惠

+----------+------------+-----------------------+
| offer_id | student_id | employer_offer        |
+----------+------------+-----------------------+
| 1        | 1          | Wayne Enterprises     |
+----------+------------+-----------------------+
| 2        | 1          | Gotham National Bank  |
+----------+------------+-----------------------+
| 3        | 2          | Wayne Enterprises     |
+----------+------------+-----------------------+

interestoffers表不一定包含每个student_id的记录,但同时包含多个引用单个student_id的记录。

对于后两个表中的每一个,我想:

  1. 选择employer_interestemployer_offer值等于$var的所有行(我在PHP中设置的变量)
  2. 将这些行加入原始表格
  3. 例如,如果$var设置为Wayne Enterprises,我希望结果表为:

    +------------+------------+-----------+-------+------+-------------------+----------------+-------------------+
    | student_id | first_name | last_name | class | rank | employer_interest | interest_level | employer_offer    |
    +------------+------------+-----------+-------+------+-------------------+----------------+-------------------+
    | 1          | John       | Doe       | 2012  | 1    | Wayne Enterprises | High           | Wayne Enterprises |
    +------------+------------+-----------+-------+------+-------------------+----------------+-------------------+
    | 2          | Suzy       | Public    | 2013  | 12   | Wayne Enterprises | Low            | Wayne Enterprises |
    +------------+------------+-----------+-------+------+-------------------+----------------+-------------------+
    | 3          | Mike       | Smith     | 2014  | 50   | NULL              | NULL           | NULL              |
    +------------+------------+-----------+-------+------+-------------------+----------------+-------------------+
    

    我正在尝试使用MySQL查询吗?如果是这样,我该怎么做?

2 个答案:

答案 0 :(得分:2)

听起来你只需要对其他表格进行LEFT JOIN,因为看起来你想看到第一套中的所有学生,无论任何工作机会/兴趣。

如果是这样的话......确保“兴趣”和“优惠”表都有一个索引,其中学生ID是单个元素索引,或者首先是复合索引。

select STRAIGHT_JOIN
      ORS.Student_ID,
      ORS.First_Name,
      ORS.Last_Name,
      ORS.Class,
      ORS.Rank,
      JI.Employer_Interest,
      JI.Interest,
      OFR.Employer_Offer
   from 
      OriginalResultSet ORS

         LEFT JOIN Interest JI
            ON ORS.Student_ID = JI.Student_ID
           AND JI.Employer_Interest = YourPHPVariable

            LEFT JOIN Offers OFR
               on JI.Student_ID = OFR.Student_ID
              AND JI.Employer_Interest = OFR.Employer_Offer

为防止“NULL”导致雇主的兴趣,兴趣和要约,您可以将它们包含在Coalesce()调用中,例如(对于左连接中的所有三列)

COALESCE( JI.Employer_Interest, " " ) Employer_Interest

答案 1 :(得分:1)

您的查询应该是这样的:

select 
    s.student_id, s.first_name, s.last_name, s.class, s.rank, 
    i.employer_interest, i.interest_level, 
    o.employer_offer 
from students s
left join interest i 
    on i.student_id = s.student_id 
    and i.employer_interest = 'Wayne Enterprises'
left join offers o 
    on o.student_id = s.student_id 
    and o.employer_offer = 'Wayne Enterprises'