简体   繁体   English

SQL | 限制条款 | 未达到限制

[英]SQL | LIMIT Clause | limit not achieved

I have the following query to get iPhones from table1 by color and limited to 10我有以下查询以按颜色从 table1 获取 iPhone 并限制为 10

SELECT * FROM table1 WHERE color = 'black' LIMIT 10

The above code works perfectly, When the black iPhones in table1 less than 10 I want to complete the iPhone's number to 10 with red color which I have also in table1 in one query上面的代码完美运行,当 table1 中的黑色 iPhone 小于 10 时,我想用红色完成 iPhone 的编号为 10,我在 table1 中也有一个查询

You can filter on both colors, and do a conditional sort:您可以同时过滤 colors,并进行条件排序:

SELECT * 
FROM table1 
WHERE color in ('black', 'red')
ORDER BY color = 'red'
LIMIT 10

Condition color = 'red' yields 1 when fulfilled, else 0 - so this actually puts 'black' first.条件color = 'red'满足时产生1 ,否则产生0 - 所以这实际上将'black'放在首位。

You can also use field() here:您也可以在此处使用field()

SELECT * 
FROM table1 
WHERE color in ('black', 'red')
ORDER BY FIELD(color, 'black', 'red')
LIMIT 10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM