2016-07-05 183 views
0

我在查詢時遇到了一些問題。我想通過一些id來查詢一個查詢。這裏是我的查詢:在MySQL查詢中使用GROUP_CONCAT和ORDER BY FIELD的問題

此查詢不返回結果,因爲我想:(

SELECT * 
FROM question q 
INNER JOIN answer a ON a.question_id = q.question_id 
WHERE q.lesson_id = 1 
AND q.question_id IN (
    SELECT e.question_id 
    FROM exame e 
    WHERE e.inscription_id = 1 
    AND e.lesson_id = 1 
    AND e.attempt = 1 
    ORDER BY e.question_id 
) /*this subquery returns (10,2,1,8,3,12,4,11,14,7)*/ 
ORDER BY FIELD(q.question_id,(
    SELECT GROUP_CONCAT(DISTINCT ee.question_id ORDER BY ee.order_of_appearance SEPARATOR ',') AS final_order 
    FROM exame ee 
    WHERE ee.inscription_id = 1 
    AND ee.lesson_id = 1 
    AND ee.attempt = 1) 
) /*this subquery returns (10,2,1,8,3,12,4,11,14,7)*/ 

正如你可以看到,兩個子查詢返回相同的結果(10,2,1 ,8,3,12,4,11,14,7),正如你所知道的,它們之間的區別在於第一個返回結果集,第二個只有一個字段與所有的id連接在一起。

問題#1:如果我複製第一個子查詢並將其寫入第二個所在的位置,則出現此錯誤:

1242 - Subquery returns more than 1 row

所以我創建了第二個子查詢(帶有GROUP_CONCAT函數),但結果並不是我期待的。結果按「question_id」排序,我希望它已按「order_of_appearance」字段排序。

問題2:如果我寫ORDER裏面的子查詢BY子句中,我沒有得到的「order_of_appearance」字段排序的結果,但如果我刪除的子查詢和我手動編寫的id的陣列,結果按「order_of_appearance」排序!爲什麼???

此查詢返回結果,因爲我願意! :)

SELECT * 
FROM question q 
INNER JOIN answer a ON a.question_id = q.question_id 
WHERE q.lesson_id = 1 
AND q.question_id IN (
    SELECT e.question_id 
    FROM exame e 
    WHERE e.inscription_id = 1 
    AND e.lesson_id = 1 
    AND e.attempt = 1 
    ORDER BY e.question_id 
) 
ORDER BY FIELD(q.question_id,10,2,1,8,3,12,4,11,14,7) 

最後一個問題:是否可以達到我想要的東西,而無需手動編寫的id的陣列?我需要做到這一點。

提前致謝! (我希望你明白我的英語!)

回答

1

我認爲你需要使用FIND_IN_SET()而不是FIELD()

從參考手冊:

FIELD(str,str1,str2,str3,...)

Returns the index (position) of str in the str1 , str2 , str3 , ... list. Returns 0 if str is not found.

If all arguments to FIELD() are strings, all arguments are compared as strings. If all arguments are numbers, they are compared as numbers. Otherwise, the arguments are compared as double.

If str is NULL , the return value is 0 because NULL fails equality comparison with any value. FIELD() is the complement of ELT() .

mysql> SELECT FIELD('ej', 'Hej', 'ej', 'Heja', 'hej', 'foo'); 
    -> 2 

mysql> SELECT FIELD('fo', 'Hej', 'ej', 'Heja', 'hej', 'foo'); 
    -> 0 

FIND_IN_SET(str,strlist)

Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by 「,」 characters. If the first argument is a constant string and the second is a column of type SET , the FIND_IN_SET() function is optimized to use bit arithmetic. Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL . This function does not work properly if the first argument contains a comma (「,」) character.

mysql> SELECT FIND_IN_SET('b','a,b,c,d'); 
    -> 2 
+0

格拉西亞斯@Barranka! !它被喚醒!我已經使用FIND_IN_SET()和suddenty我有我想要的結果!非常感謝!! Saludos desdeEspaña! – user3800667

+0

@ user3800667樂於助人。 Por cierto,tambiénhay una comunidad de StackOverflow enespañol:http://es.stackoverflow.com – Barranka

+0

gracias @Barranka! Leecharéun ojo! – user3800667