首先,你的表架構是不是最大的。一個更好的模式是:
Match | Set | W | L
---------------------
1 1 6 7
1 2 6 3
1 3 5 7
1 4 3 6
2 1 6 7
2 2 6 7
2 3 6 2
2 4 3 6
然後將查詢將是:
SELECT count(*) FROM <table> WHERE (W=6 AND L=7) OR (W=7 AND L=6)
因此,我們可以讓你的表格看起來像一個通過SQL上方,然後使用SQL作爲作弊上面的SQL中的一個子查詢。爲了讓你看起來像雷:
SELECT 1 as set, W1 as W, L1 as L FROM <table>
UNION ALL
SELECT 2, W2 as W, L2 as L FROM <table>
UNION ALL
SELECT 3, W3 as W, L3 as L FROM <table>
UNION ALL
SELECT 4, W4 as W, L4 as L FROM <table>
UNION ALL
SELECT 5, W5 as W, L5 as L FROM <table>
結合他們:
SELECT count(*)
FROM (
SELECT 1 as set, W1 as W, L1 as L FROM <table>
UNION ALL
SELECT 2, W2 as W, L2 as L FROM <table>
UNION ALL
SELECT 3, W3 as W, L3 as L FROM <table>
UNION ALL
SELECT 4, W4 as W, L4 as L FROM <table>
UNION ALL
SELECT 5, W5 as W, L5 as L FROM <table>
) as unionSub
WHERE (W=6 AND L=7) OR (W=7 AND L=6)
也許你可以做一個觀點指出,聯合查詢的,因爲我會用它在未來引用此表想象你最終會經常使用它。
「所以我有結構化這樣的網球成績表:」 是的。修復。見正常化。 – Strawberry
你有關於網球成績結構的建議嗎? –
數據存儲與數據檢索和顯示混淆。一個典型的結構可能是:'match_id,set_no,player1_score,player2_score' – Strawberry