2016-07-05 139 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