2

这里有人帮助整理了这个非常有用的查询!

SELECT * FROM results r 
INNER JOIN categories c on r.id = c.result_id 
WHERE c.name in ('$purpose', '$prop_type', '$loan_type')
GROUP BY r.id
HAVING COUNT(c.c_id) = 3
LIMIT 50 OFFSET 0

哪个很好用!但是,当我尝试使用

SELECT * FROM results r 
INNER JOIN categories c on r.id = c.result_id 
WHERE c.name in ('$purpose', '$prop_type', '$loan_type')
ORDER BY r.usefulness DESC
GROUP BY r.id
HAVING COUNT(c.c_id) = 3
LIMIT 50 OFFSET 0

我收到语法错误。我在这里遗漏了一些明显的东西吗?

非常感谢!

4

2 回答 2

3

查询的各个部分有一个顺序,并且ORDER BY应该在最后(在LIMIT和之前OFFSET......试试这个:

SELECT * FROM results r 
INNER JOIN categories c on r.id = c.result_id 
WHERE c.name in ('$purpose', '$prop_type', '$loan_type')
GROUP BY r.id
HAVING COUNT(c.c_id) = 3
ORDER BY usefulness DESC
LIMIT 50 OFFSET 0

还要确保在任何行的末尾都有空格...

请注意,在 mysql 中,这是一个缩短版本LIMIT x OFFSET y- 你可以写LIMIT y,x

于 2012-04-14T00:59:45.543 回答
1

ORDER BY必须在之后HAVING

于 2012-04-14T00:58:59.137 回答