的最大數目前我有9列的MySQL尋找從一組列
Brand, ID, M, T, W, Th, Fri, Sat, Sun
abc, 1, 2, 4, 6, 3, 7, 1, 5
我想找出以下組的具有最高單獨計數。 m/t/w/th,星期五,星期六/星期日。例如在上表中,星期五將被選中,因爲它具有最高的計數。
的最大數目前我有9列的MySQL尋找從一組列
Brand, ID, M, T, W, Th, Fri, Sat, Sun
abc, 1, 2, 4, 6, 3, 7, 1, 5
我想找出以下組的具有最高單獨計數。 m/t/w/th,星期五,星期六/星期日。例如在上表中,星期五將被選中,因爲它具有最高的計數。
假設值沒有關係,可以用greatest
和case
表達式來做到這一點。如果有關係,則會返回case
表達式中遇到的第一個值(在連續的幾天之內)。
select case greatest(M, T, W, Th, Fri, Sat, Sun)
when M then 'Monday'
when T then 'Tuesday'
when W then 'Wednesday'
when Th then 'Thursday'
when Fri then 'Friday'
when Sat then 'Saturday'
when Sun then 'Sunday'
end
from tbl
SELECT BRAND, ID, GREATEST (M, T, W, Th, Fri, Sat, Sun) Max_day
FROM yourtable;
select brand, ID,
case
when M>T and M>W and M>Th and M>Fri and M>Sat and M>Sun then M
when T>M and T>W and T>Th and T>Fri and T>Sat and T>Sun then T
when W>T and W>M and W>Th and W>Fri and W>Sat and W>Sun then W
when TH>T and TH>W and TH>M and TH>Fri and TH>Sat and TH>Sun then TH
when FRI>T and FRI>W and FRI>Th and FRI>M and FRI>Sat and FRI>Sun then FRI
when SAT>T and SAT>W and SAT>Th and SAT>Fri and SAT>M and SAT>Sun then SAT
when SUN>T and SUN>W and SUN>Th and SUN>Fri and SUN>Sat and SUN>M then SUN
ELSE M END AS MAXDAY
FROM TBL1;
認真考慮您的規範化模式。 – Strawberry
在這種情況下,'/'和','有什麼區別。 – Strawberry