1

我有以下 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。

现在我只需要“case”为 SET 的 20 个第一个结果和“case”为 EMPTY 的 100 个结果。

如果我执行以下操作:

"SELECT id, case, value 
FROM tbl1 ... 
ORDER BY case DESC,value DESC 
LIMIT 0,120"

它给了我设置“case”的所有值......但我只想要 20。

4

2 回答 2

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
于 2013-03-22T17:48:58.083 回答
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
于 2013-03-22T17:49:37.790 回答