[英]MySQL join not working
I have the following query. 我有以下查询。
SELECT DISTINCT tbl_event_entries.race_nbr,
tbl_event_entries.class_id,
tbl_event_entries.racer_id,
tbl_event_entries.heat_position,
tbl_event_classes.combine
FROM tbl_event_entries
LEFT JOIN tbl_event_classes
ON tbl_event_entries.class_id = tbl_event_classes.class_id
WHERE tbl_event_entries.event_id = :event_id
ORDER BY tbl_event_entries.race_nbr, tbl_event_entries.heat_position
The data that this is in this query is : 该查询中的数据是:
event_class_id event_id class_id combine
822 20160706MAS577d098260173 1 2
823 20160706MAS577d098260173 2 8888
entry_id event_id racer_id class_id
8318 20160706MAS577d098260173 238 1
8319 20160706MAS577d098260173 1184 2
The output that I get from this query: 我从此查询中获得的输出:
race_nbr class_id racer_id heat_position combine
1 1 238 1 2
1 1 238 1 NULL
2 2 1184 1 8888
2 2 1184 1 NULL
I can limit the query to not show "NULL" in combined, but I don't always have data in that field. 我可以将查询限制为在组合中不显示“ NULL”,但我并不总是在该字段中有数据。 But, I should never see duplicated rows like this.
但是,我永远都不会看到像这样的重复行。
I should see just two rows: 我应该只看到两行:
race_nbr class_id racer_id heat_position combine
1 1 238 1 2
2 2 1184 1 8888
Can anyone see the error? 谁能看到错误?
I have tried LEFT JOIN, JOIN and a few different changes with the fields. 我已经尝试了LEFT JOIN,JOIN和字段的一些不同更改。 Not seeing why this is returning the invalid data..
没有看到为什么这会返回无效数据。
Answer: Use aggregation... 答:使用聚合...
SELECT e.race_nbr
, e.class_id
, e.racer_id
, e.heat_position
, MAX(c.combine) combine
FROM tbl_event_entries e
LEFT
JOIN tbl_event_classes c
ON c.class_id = e.class_id
WHERE e.event_id = :event_id
GROUP
BY e.race_nbr
, e.class_id
, e.racer_id
, e.heat_position
ORDER
BY e.race_nbr
, e.heat_position
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.