[英]How to get SQL query HAVING count display BOTH records with the same value where INNER JOIN exists
這是當前的查詢:
SELECT schedule.routenr, stops.stopname, schedule.scheduletime FROM schedule
INNER JOIN stops ON
schedule.id_stop=stops.id_stop
INNER JOIN tram ON
schedule.id_tram=tram.id_tram
WHERE tram.id_direction = '5' AND stops.stopname = 'Stourton' AND
schedule.scheduletime >= ('10:50:00')
OR tram.id_direction = '5' AND stops.stopname = 'CitySquare'
ORDER BY schedule.routenr ASC, schedule.scheduletime ASC;
這些是顯示的結果:
route_nr stopname scheduletime
1 CitySquare 09:57:00
2 Stourton 11:50:00
2 CitySquare 11:57:00
3 Stourton 12:50:00
3 CitySquare 12:57:00
查詢結果添加了HAVING COUNT(schedule.routenr)> 1
route_nr stopname scheduletime
2 Stourton 11:50:00
我希望能夠在route_nr中顯示具有重復計數的兩個記錄,以便顯示stopname = Stourton和stopname = CitySquare。 這是它應該顯示的內容。
route_nr stopname scheduletime
2 Stourton 11:50:00
2 CitySquare 11:57:00
3 Stourton 12:50:00
3 CitySquare 12:57:00
這是包含數據的表列表:
tram stops
id_tram id_direction id_stop stopname
1 1 1 GrimesDyke
2 1 2 SeacroftRingRoad
3 1 3 WykeBeck
4 1 4 FfordeGrene
5 2 5 St.James'sHospital
6 2 6 QuarryHill
7 2 7 Eastgate
8 2 8 CitySquare
9 2 9 Bodington
10 2 10 HeadingleyCentre
11 3 11 HydeParkCorner
12 3 12 UniversityofLeeds
13 3 13 Civic
14 4 14 Riverside
15 5 15 ClarenceDock
16 StJoseph's
17 ChurchStreet
18 Stourton
19 BalmRoad
20 BelleIsleNorth
21 BelleIsleCentral
22 BelleIsleSouth
23 MiddletonDistrictCentre
24 MiddletonCircus
25 Tingley
schedule
id_schedule id_tram id_stop routenr scheduletime
1 15 18 1 09:50:00
2 15 18 2 11:50:00
3 15 18 3 12:50:00
4 15 8 1 09:57:00
5 15 8 2 11:57:00
6 15 8 3 12:57:00
7 15 8 1 09:42:00
8 14 18 1 09:49:00
9 14 8 2 11:42:00
10 14 18 2 11:49:00
也許將每個route_nr
的記錄放在route_nr
上就足夠了。 如果是這樣的話:
SELECT s.routenr, GROUP_CONCAT(st.stopname ORDER BY s.scheduletime) as stopnames,
GROUP_CONCAT(s.scheduletime ORDER BY s.scheduletime) as schedules
FROM schedule s INNER JOIN
stops st
ON s.id_stop = st.id_stop INNER JOIN
tram t
ON s.id_tram = t.id_tram
WHERE (t.id_direction = '5' AND st.stopname = 'Stourton' AND
s.scheduletime >= '10:50:00') OR
(t.id_direction = '5' AND st.stopname = 'CitySquare')
GROUP BY s.routenr
HAVING COUNT(*) > 1;
如果你真的想要原始的行,那么你需要一個更復雜的查詢,這在MySQL中很麻煩(但在其他數據庫中卻沒有)。 一種方法是重復外部查詢的邏輯:
SELECT s.routenr, st.stopname, s.scheduletime
FROM schedule s INNER JOIN
stops st
ON s.id_stop = st.id_stop INNER JOIN
tram t
ON s.id_tram = t.id_tram
WHERE ((t.id_direction = '5' AND st.stopname = 'Stourton' AND
s.scheduletime >= '10:50:00') OR
(t.id_direction = '5' AND st.stopname = 'CitySquare')
) AND
s.routenr IN (SELECT s.routenr
FROM schedule s INNER JOIN
stops st
ON s.id_stop = st.id_stop INNER JOIN
tram t
ON s.id_tram = t.id_tram
WHERE (t.id_direction = '5' AND st.stopname = 'Stourton' AND s.scheduletime >= '10:50:00') OR
(t.id_direction = '5' AND st.stopname = 'CitySquare')
GROUP BY s.routenr
HAVING COUNT(*) > 1
);
SELECT schedule.routenr, stops.stopname, schedule.scheduletime FROM schedule
INNER JOIN stops ON
schedule.id_stop=stops.id_stop
INNER JOIN tram ON
schedule.id_tram=tram.id_tram
WHERE tram.id_direction = '5' AND stops.stopname = 'Stourton' AND
schedule.scheduletime >= ('10:50:00') OR tram.id_direction = '5' AND
stops.stopname = 'CitySquare' AND schedule.routenr
in (SELECT s.routenr FROM schedule s
INNER JOIN stops ON
schedule.id_stop=stops.id_stop
INNER JOIN tram ON
schedule.id_tram=tram.id_tram
WHERE tram.id_direction = '5' AND stops.stopname = 'Stourton' AND
schedule.scheduletime >= ('10:50:00')
OR tram.id_direction = '5' AND stops.stopname = 'CitySquare'
GROUP BY s.routenr
HAVING COUNT(*) > 1);
你可以用這個:
select tmp.routenr, maintable.stopname, maintable.scheduletime
from(
SELECT sc.routenr, st.stopname, sc.scheduletime
FROM schedule sc
INNER JOIN stops st
ON sc.id_stop = st.id_stop
INNER JOIN tram tr
ON sc.id_tram = tr.id_tram
WHERE tr.id_direction = '5' AND st.stopname = 'Stourton' AND
sc.scheduletime >= ('10:50:00')
OR tr.id_direction = '5' AND st.stopname = 'CitySquare'
) maintable
inner join (
SELECT sc1.routenr, count(*) as routenr_count
FROM schedule sc1
INNER JOIN stops st1
ON sc1.id_stop = st1.id_stop
INNER JOIN tram
ON sc1.id_tram = tr1.id_tram
WHERE tr1.id_direction = '5' AND st1.stopname = 'Stourton' AND
sc1.scheduletime >= ('10:50:00')
OR tr1.id_direction = '5' AND st1.stopname = 'CitySquare'
group by sc1.routenr
) tmp
on tmp.routenr = maintable.routenr
where tmp.routenr_count > 1
ORDER BY maintable.routenr ASC, maintable.scheduletime ASC;
我想知道or
條款,你能證實它是對的嗎? 因為我覺得應該是這樣的
where (tram.id_direction = '5' AND stops.stopname = 'Stourton' AND
schedule.scheduletime >= ('10:50:00')
)
OR
(tram.id_direction = '5' AND stops.stopname = 'CitySquare'
)
編輯:已更改,因此它可用作示例數據。 另一個解決方案應該是來自Gordon Linoff
( route_nr in (...)
第二個解決方案route_nr in (...)
在他的查詢中首先糾正你的OR
意圖)
解決此問題的另一種方法是使用變量模擬row_number
,以按升序和降序按schedule_time
枚舉每個route_nr
組中的行。 其中一個行號大於1的所有行都是具有多於1行的route_nr
組的一部分:
SELECT * FROM (
SELECT * ,
@prevc2 := IF(@prev2 = routenr, @prevc2+1, 1) rn2,
@prev2 := routenr
FROM (
SELECT schedule.routenr, stops.stopname, schedule.scheduletime,
@prevc1 := IF(@prev1 = schedule.routenr, @prevc1+1, 1) rn1,
@prev1 := schedule.routenr
FROM schedule
INNER JOIN stops ON
schedule.id_stop=stops.id_stop
INNER JOIN tram ON
schedule.id_tram=tram.id_tram
WHERE tram.id_direction = '5' AND stops.stopname = 'Stourton' AND
schedule.scheduletime >= ('10:50:00')
OR tram.id_direction = '5' AND stops.stopname = 'CitySquare'
ORDER BY schedule.routenr ASC, schedule.scheduletime ASC
) t
ORDER BY routenr ASC, scheduletime DESC
) t WHERE rn1 > 1 OR rn2 > 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.