[英]How to select in a set of records having 0 or 1 value in a field, the record with value 1 only if exists, in mysql?
我有一個表“ wordstyped”,其中包含“ idBook”,“ guidUser”,“ bookmarkLetter”,“ letter”,“ attemptWrong”和“ isCorrect”。
idBook guidUser bookmarkLetter letter attemptWrong isCorrect
-------------------------------------------------------------------
1 1 100 a 2 0
1 1 100 a 3 0
1 1 100 a 3 1
1 1 101 b 6 0
1 1 101 b 2 0
2 2 101 b 3 0
2 3 152 d 7 0
3 3 153 e 2 0
我想選擇所有記錄,它們的所有字段都包含最大數量的“ attemptWrong”,但三元組分別為“ idBook”,“ guidUser”和“ bookmarkLetter”。 我已經用這個查詢完成了
SELECT DISTINCT w1.bookmarkletter, w1.attemptsWrong
FROM wordstyped w1 INNER JOIN ( SELECT bookmarkLetter, guidUser, idBook,
MAX(attemptsWrong) AS maxAttemptsWrong
FROM wordstyped
GROUP BY bookmarkLetter, guidUser, idBook ) w2
ON w1.bookmarkLetter = w2.bookmarkLetter AND w1.guidUser = w2.guidUser
AND w1.guidUser = '1'
AND w1.idBook ='1'
AND w1.attemptsWrong = w2.maxAttemptsWrong AND w1.bookmarkLetter >=0
AND w1.bookmarkLetter <=200 ORDER BY `w1`.`bookmarkletter` ASC
就像這個小提琴一樣http://sqlfiddle.com/#!9/238794/2
現在,我也要選擇“ isCorrect”,如果至少一個記錄的ha值為isCorrect = 1,則選擇值為1;如果所有記錄的值為0,則選擇0。
如果我在示例情況下將w1.isCorrect添加到第一個select語句中
SELECT DISTINCT w1.bookmarkletter, w1.attemptsWrong, w1.isCorrect
FROM wordstyped w1 INNER JOIN ( SELECT bookmarkLetter, guidUser, idBook,
MAX(attemptsWrong) AS maxAttemptsWrong
FROM wordstyped
GROUP BY bookmarkLetter, guidUser, idBook ) w2
ON w1.bookmarkLetter = w2.bookmarkLetter AND w1.guidUser = w2.guidUser
AND w1.guidUser = '1'
AND w1.idBook ='1'
AND w1.attemptsWrong = w2.maxAttemptsWrong AND w1.bookmarkLetter >=0
AND w1.bookmarkLetter <=200 ORDER BY `w1`.`bookmarkletter` ASC
( http://sqlfiddle.com/#!9/238794/1 ),這組記錄
idBook guidUser bookmarkLetter letter attemptWrong isCorrect
-------------------------------------------------------------------
1 1 101 b 6 0
1 1 101 b 2 0
2 2 101 b 3 0
正確返回
bookmarkLetter attemptWrong isCorrect
-------------------------------------------------------------------
101 6 0
而這組記錄
idBook guidUser bookmarkLetter letter attemptWrong isCorrect
-------------------------------------------------------------------
1 1 100 a 2 0
1 1 100 a 3 0
1 1 100 a 3 1
回報
bookmarkLetter attemptWrong isCorrect
-------------------------------------------------------------------
100 3 0
100 3 1
代替
bookmarkLetter attemptWrong isCorrect
-------------------------------------------------------------------
100 3 1
如何獲得第二個結果?
編輯:我有isCorrect 1的情況總是像示例中一樣發生,即我有兩條帶有tryWrong的記錄,這是最大值,一個記錄是isCorrect = 0,一個記錄是isCorrect = 1
嘗試使用匯總和分組依據
http://sqlfiddle.com/#!9/238794/4
SELECT w1.bookmarkletter, max(w1.attemptsWrong), max(w1.isCorrect)
FROM wordstyped w1 INNER JOIN
( SELECT bookmarkLetter, guidUser, idBook, MAX(attemptsWrong) AS maxAttemptsWrong
FROM wordstyped
GROUP BY bookmarkLetter, guidUser, idBook ) w2
ON w1.bookmarkLetter = w2.bookmarkLetter AND w1.guidUser = w2.guidUser
AND w1.guidUser = '1'
AND w1.idBook ='1'
AND w1.attemptsWrong = w2.maxAttemptsWrong AND w1.bookmarkLetter >=0
AND w1.bookmarkLetter <=200
group by w1.bookmarkletter
ORDER BY `w1`.`bookmarkletter` ASC
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.