从一张表中选择不同的值并限制它们

Posted

技术标签:

【中文标题】从一张表中选择不同的值并限制它们【英文标题】:Select from one table different values and limit them 【发布时间】:2013-04-16 03:29:03 【问题描述】:

我有一张桌子:

first_column | last_column
--------------------------
text         | text
text         | text
text         | NO text

现在我想从这个表中选择 LIMIT 80 其中last_column 是“文本”和 LIMIT 80 WHERE last_column 是“无文本”

我的问题是“NO text”只能有 79 行。

请对一个查询提供一些帮助?

【问题讨论】:

NO text 只能有 79 行这是你的问题吗?你能解释一下吗? 【参考方案1】:

由于您想为textNO text 获得80 行,因此可以使用UNION ALL。您还可以根据您的要求对数据进行排序:

(SELECT first_column, last_column
FROM MyTable
WHERE last_column = 'text'
ORDER BY first_column
LIMIT 80)
UNION ALL
(SELECT first_column, last_column
FROM MyTable
WHERE last_column = 'NO text'
ORDER BY first_column
LIMIT 80);

See this SQLFiddle

【讨论】:

以上是关于从一张表中选择不同的值并限制它们的主要内容,如果未能解决你的问题,请参考以下文章