-2

错误在哪里?

SELECT * FROM papiry 
WHERE (aktivni=1 AND pocetovereni>1 ORDER BY pocetovereni DESC LIMIT1) 
AND oddeleni='$oddeleni' ORDER BY nazev ASC;

感谢您的帮助,我不擅长 SQL。

4

3 回答 3

1

你有一个糟糕的 SQL,ORDER BY不是要在子句中输入的WHERE子句。一个潜在的优秀 SQl 看起来像这样:

SELECT * FROM papiry 
WHERE aktivni=1 
AND pocetovereni>1 
AND oddeleni='$oddeleni' 
ORDER BY ORDER BY pocet, overeni DESC, nazev ASC;

但是,您必须决定要按哪些列排序。这是在 SQL 命令的末尾定义的。

于 2012-09-18T14:58:25.217 回答
1
SELECT * 
FROM papiry 
WHERE (aktivni = 1 AND pocetovereni > 1) 
AND oddeleni='$oddeleni' 
ORDER BY 
     CASE WHEN ktivni = 1 AND pocetovereni > 1 THEN pocetovereni END DESC,
     CASE WHEN oddeleni='$oddeleni' THEN nazev END ASC
于 2012-09-18T14:58:51.533 回答
0

Order By 必须出现在 WHERE 子句之后,这至少在 SQL Server 中有效:

SELECT * FROM papiry 
WHERE aktivni=1 AND pocetovereni>1 AND oddeleni='$oddeleni' 
ORDER BY pocetovereni DESC, nazev ASC
于 2012-09-18T15:01:33.763 回答