gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:39:50 25 4
gpt4 key购买 nike

我有多个包含不同列数的 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 查询是否可以实现我正在尝试做的事情?如果可以,我该怎么做?

最佳答案

听起来您只需要对其他表进行 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”结果出现在 employer interest、interest 和 offer 中,您可以将它们包装在 Coalesce() 调用中,例如(对于左连接上的所有三列)

COALESCE( JI.Employer_Interest, " " ) Employer_Interest

关于mysql - 仅加入值与变量匹配的特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7630372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com