【问题标题】:Join only for specific rows where value matches a variable仅加入值与变量匹配的特定行
【发布时间】:2011-10-03 01:29:14
【问题描述】:

我有多个 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. 将这些行加入到原始表中

例如,如果 $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 查询是否可行?如果是这样,我该怎么做?

【问题讨论】:

    标签: mysql select join


    【解决方案1】:

    听起来您只需要 LEFT JOIN 到其他表格,因为您似乎希望看到第一组中的所有学生,而不管任何工作机会/兴趣。

    如果是这样...确保“Interest”和“Offers”表都有一个索引,其中学生 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
    

    【讨论】:

    • 这太完美了——我什至没有尝试这个,因为出于某种原因,我认为解决方案更复杂。谢谢!
    • @DRapp:有什么解释你为什么建议使用STRAIGHT_JOIN 吗?
    • @ypercube,我猜是习惯的力量......我以前对一个看似简单的连接表有 14+ 百万条记录和优化器试图为我思考和阻塞了查询,以更好地了解我的数据应该如何工作。添加直接连接总是有帮助的,因为我尝试让最顶层的表(或第一个 SQL)返回最小的记录集,然后加入查询中的其余表以获得最终结果。
    • 问题不相似,但我明白了。谢谢:)
    【解决方案2】:

    您的查询应该是这样的:

    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'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多