我有以下sql查询:
"SELECT id, case, value
FROM tbl1
WHERE (case > '100' OR case is null)
ORDER BY case DESC, value DESC"
结果是这样的:
| id | case | value |
--------------------------
| 17 | case1 | value1 |
| 16 | case2 | value2 |
| 14 | case3 | value3 |
| 18 | empty | value4 |
| 15 | empty | value5 |
它为我提供了100个结果,其中设置了“case”。 “案件”为空的500。
现在我只需要20个第一个结果,其中“case”是SET,100个结果,其中“case”是EMPTY。
如果我执行以下操作:
"SELECT id, case, value
FROM tbl1 ...
ORDER BY case DESC,value DESC
LIMIT 0,120"
它给了我设置“case”的所有值......但我只想要20。
答案 0 :(得分:1)
你最好结合2个查询
SELECT id, case, value
FROM tbl1
WHERE CASE IS NOT NULL
LIMIT 20
UNION
SELECT id, case, value
FROM tbl1
WHERE CASE IS NULL
ORDER BY case DESC
LIMIT 100
答案 1 :(得分:0)
使用union
SELECT id, case, value
FROM tbl1
WHERE case > '100'
ORDER BY case DESC, value DESC
limit 20
union
SELECT id, case, value
FROM tbl1
WHERE case is null
ORDER BY case DESC, value DESC
limit 100