简体   繁体   English

在该查询中,我使用哪种JOIN类型显示NULL值?

[英]Which JOIN type do I use to display NULL values in this query?

I'm trying to join THREE tables. 我正在尝试加入三个桌子。

The first table is transactions which contains details of "points" that students have earned. 第一个表是transactions ,其中包含学生获得的“积分”的详细信息。 The relevant field here is Recipient_ID (the student receiving points). 此处的相关字段是Recipient_ID (学生接收点)。

The second table is purchases which contains details of "rewards" that students have purchased. 第二个表是purchases ,其中包含学生购买的“奖励”的详细信息。 The relevant fields here are Student_ID and Reward_ID . 这里的相关字段是Student_IDReward_ID

The final table is rewards which details the rewards purchased. 最终表是rewards ,其中详细说明了购买的奖励。 The relevant fields here are Reward_ID , Cost_to_User (the price the students pay for each reward; I'm currently not using this at all) and Title . 这里的相关字段是Reward_IDCost_to_User (学生为每种奖励支付的价格;我目前根本没有使用此价格)和Title


What I'm trying to do, provided with a specific list of student IDs, is display a list of students' points AND three most expensive reward purchases. 我想要做的是,提供特定的学生ID列表,以显示学生的积分列表和三个最昂贵的奖励购买。

The SQL statement I've written thus far is: 到目前为止,我编写的SQL语句是:

SELECT
  t.Recipient_ID AS  `ID` ,
  SUM( t.Points ) AS  `Points Earned`,
  SUBSTRING_INDEX( GROUP_CONCAT(DISTINCT r.Title SEPARATOR ', '),', ',3 ) as `Rewards`
FROM  `transactions` t
JOIN `purchases` p ON t.Recipient_ID = p.Student_ID
JOIN `rewards` r ON p.Reward_ID = r.Reward_ID
WHERE  T.`Recipient_ID` 
  IN ( 90128, 90163, 34403, 35501 )
GROUP BY  t.`Recipient_ID`

This works to an extent - two students are displayed, with their point totals and latest rewards. 这在一定程度上有效-显示了两名学生,以及他们的总分和最新奖励。

However, their point totals are vastly wrong, and I believe students aren't displaying if they haven't made any purchases. 但是,他们的积分总数非常错误,我相信学生们如果没有购买任何物品,就不会显示出来。

I would still like to display the students if they've made no purchases. 如果他们没有购物,我仍然想向他们展示。


I suspect it's my JOINs which are incorrect, but I also suspect GROUP_CONCAT isn't powerful enough to list the three most EXPENSIVE rewards. 我怀疑这是我的JOIN错误,但我也怀疑GROUP_CONCAT的功能不足以列出三个最昂贵的奖励。 Do I need to change my JOINs, or do I need to use some sort of subquery? 我需要更改我的JOIN,还是需要使用某种子查询?

Thanks in advance, 提前致谢,

I think you should look at using a LEFT JOIN (See Visual Explanation of Joins ): 我认为您应该看一下使用LEFT JOIN (请参阅Join的视觉说明 ):

SELECT
  t.Recipient_ID AS  `ID` ,
  SUM( t.Points ) AS  `Points Earned`,
  SUBSTRING_INDEX( GROUP_CONCAT(DISTINCT r.Title SEPARATOR ', '),', ',3 ) as `Rewards`
FROM  `transactions` t
LEFT JOIN `purchases` p 
   ON t.Recipient_ID = p.Student_ID
LEFT JOIN `rewards` r 
  ON p.Reward_ID = r.Reward_ID
WHERE  T.`Recipient_ID` 
  IN ( 90128, 90163, 34403, 35501 )
GROUP BY  t.`Recipient_ID`

A LEFT JOIN will return all records from the transactions table even if there are no records in the purchases or rewards table. LEFT JOIN将返回transactions表中的所有记录,即使purchasesrewards表中没有记录也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM