[英]MySQL Query - SELECT 2 rows with 2 specific DISTINCT column values
我的目標是,對於每個PID
,選擇2條記錄, test_sname
值分別在相同entry_date
上出現' want '和' want2 '。 我在包含兩個test_snames
的前5個entry_dates
中執行此test_snames
。
這是我完成此查詢:
queryBuilder =
"""select PID, test_sname, test_value, units, ref_range, entry_date from labs
where PID=%s and (test_sname='want' or test_sname='want2') and entry_date in
(select entry_date from labs where PID=%s and test_sname in ('want', 'want2')
group by entry_date having count(*) = 2)
order by entry_date limit 10;""" % (pid, pid)
當entry_date只有兩行包含test_sname
為' want '或' want2 '時,它將按預期工作。
PID |test_sname |test_value |units |entry_date
10000000 | want | 343 | U/L | 2008-01-01 01:01:01
10000000 | want2 | 984.34 | | 2008-01-01 01:01:01
10000000 | NA1 | 56 | % | 2008-01-01 01:01:01
10000000 | NA2 | 420 | mg/dL | 2008-01-01 01:01:01
10000000 | NA2 | 420 | mg/dL | 2008-01-02 01:01:01
10000000 | want | 343 | U/L | 2008-01-02 01:01:01
10000000 | want2 | 984.34 | | 2008-01-02 01:01:01
10000000 | NA1 | 26 | % | 2008-01-02 01:01:01
10000000 | NA2 | 410 | mg/dL | 2008-01-02 01:01:01
10000000 | NA2 | 455 | mg/dL | 2008-01-02 01:01:01
查詢結果(正確):
PID |test_sname |test_value |units |entry_date
10000000 | want | 343 | U/L | 2008-01-01 01:01:01
10000000 | want2 | 984.34 | | 2008-01-01 01:01:01
10000000 | want | 343 | U/L | 2008-01-02 01:01:01
10000000 | want2 | 984.34 | | 2008-01-02 01:01:01
例如,當在同一entry_date上來自test_sname
的' want '多個行時,就會出現問題,因為having count(*) = 2
不再有效。 像這樣的數據沒有結果。
PID |test_sname |test_value |units |entry_date
11111111 | want | 343 | U/L | 2009-10-26 07:25:00
11111111 | want2 | 984.34 | | 2009-10-26 07:25:00
11111111 | want | 189 | U/L | 2009-10-26 07:25:00
11111111 | NA1 | 50 | % | 2009-10-26 07:25:00
11111111 | NA2 | 40 | mg/dL | 2009-10-26 07:25:00
11111111 | NA3 | 84.55 | | 2009-10-26 07:25:00
11111111 | NA4 | 4.5 | thou/uL | 2009-10-26 07:25:00
11111111 | NA5 | 14.6 | g/dL | 2009-10-26 07:25:00
11111111 | NA6 | 0.96 | mg/dL | 2009-10-26 07:25:00
11111111 | want | 343 | U/L | 2009-10-30 07:25:00
11111111 | want2 | 984.34 | | 2009-10-30 07:25:00
11111111 | want | 189 | U/L | 2009-10-30 07:25:00
11111111 | NA1 | 6 | % | 2009-10-30 07:25:00
11111111 | NA2 | 40 | mg/dL | 2009-10-30 07:25:00
11111111 | NA3 | 84.55 | | 2009-10-30 07:25:00
11111111 | NA4 | 4.5 | thou/uL | 2009-10-30 07:25:00
11111111 | NA5 | 14.6 | g/dL | 2009-10-30 07:25:00
11111111 | NA6 | 0.96 | mg/dL | 2009-10-30 07:25:00
作為限制,我嘗試在子查詢中設置limit 2
(我知道它本身無法解決問題),但是它給出了此錯誤,並且我認為我擁有SQL的最新版本,因此顯然我可以在子查詢中不使用limit
。
This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
我意識到有多種方法可以解決此問題-我可以選擇所有值,然后以編程方式獲取Python所需的內容,但是我正在尋找使用Python mySQL-connector編寫的mySQL查詢解決方案。 我不會抱怨python解決方案。
我正在將python v3.4.4與mySQL-connector v2.1.3和MySQL服務器v5.7.11一起使用
謝謝你的時間!
考慮通過子查詢使用分組的運行計數。 然后,在RowNo為1或2的位置進行過濾。這樣,您將無需傳遞參數,因為將處理所有PID。 下面假設labs表具有唯一的標識符ID
:
SELECT *
FROM
(SELECT PID, test_sname, test_value, units, ref_range, entry_date,
(SELECT count(*) FROM labs sub
WHERE sub.test_sname in ('want', 'want2')
AND sub.PID = labs.PID
AND sub.entry_date = labs.entry_date
AND sub.ID <= labs.ID) As RowNo
FROM labs
WHERE test_sname in ('want', 'want2')
) As dT
WHERE dT.RowNo <= 2
# PID test_sname test_value units ref_range entry_date RowNo
# 10000000 want 33 U/L 4-40 2008-01-01 01:01:01 1
# 10000000 want2 98.34 2008-01-01 01:01:01 2
# 10000000 want 33 U/L 4-40 2008-01-02 01:01:01 1
# 10000000 want2 98.34 2008-01-02 01:01:01 2
# 11111111 want 33 U/L Apr-40 2009-10-26 07:25:00 1
# 11111111 want2 98.34 2009-10-26 07:25:00 2
# 11111111 want 33 U/L Apr-40 2009-10-30 07:25:00 1
# 11111111 want2 98.34 2009-10-30 07:25:00 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.