简体   繁体   English

SQL选择除具有最高值的列之外的所有行

[英]SQL Selecting all rows except the column with the highest value

Here's my table(blog) with all the output shown via SELECT *这是我的表(博客),其中包含通过 SELECT * 显示的所有输出

I'm trying to select all execpt the highest value in the blogid column我正在尝试在 blogid 列中选择所有 execpt 最高值

I tried using:我尝试使用:

SELECT *
FROM blog
WHERE blogid < MAX(blogid) 
ORDER BY createddate DESC

where I hoped it selects all values that is below the max number in blogid but this gets a error "Invalid use of group function".我希望它选择 blogid 中低于最大数量的所有值,但这会出现错误“无效使用组功能”。

any help will be appreciated任何帮助将不胜感激

在此处输入图片说明

If you are not using group by, you will need a subquery to calculate the max value: 如果不使用分组依据,则将需要一个子查询来计算最大值:

SELECT *
FROM blog
WHERE blogid <  (select MAX(blogid) 
                 from blog)
ORDER BY createddate DESC

you can use not in 你可以不用

SELECT *
FROM blog
WHERE blogid  not in  ( select MAX(blogid)  from blog)
ORDER BY createddate DESC

for selecting max blogid you have to use subquery 用于选择最大Blogid,您必须使用子查询

You can use HAVING with the nested query. 您可以将HAVING与嵌套查询一起使用。 Aggregates in WHERE clause may not appear unless it is in a subquery contained in a HAVING clause or a select list 除非它在HAVING子句或选择列表中包含的子查询中,否则WHERE子句中的聚合可能不会出现

SELECT *
FROM blog
HAVING blogid < (select MAX(blogid) from blog)
ORDER BY createddate DESC

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

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