简体   繁体   English

SQL 查询 Select 除最大值外的所有内容

[英]SQL Query to Select Everything Except the Max Value

I have this rather complex query that grabs data from three tables, and now I want it to be even more complicated (Oh dear)!我有一个相当复杂的查询,它从三个表中获取数据,现在我希望它更复杂(天哪)!

I'd like the last posted feature to be displayed in it's own section of the page, and that's pretty easy by selecting the last entry in the table.我希望最后发布的功能显示在它自己的页面部分,通过选择表格中的最后一个条目,这很容易。 However, for the complex query (the main page of the site), I'd like to be able to NOT have this feature displayed.但是,对于复杂的查询(网站的主页),我希望能够不显示此功能。

I'd like to union the following query to my previous query, but it isn't returning the correct results:我想将以下查询union到我以前的查询中,但它没有返回正确的结果:

SELECT
    features.featureTitle AS title, 
    features.featureSummary AS body, 
    features.postedOn AS dummy, 
    DATE_FORMAT( features.postedOn,  '%M %d, %Y' ) AS posted, 
    NULL, 
    NULL, 
    staff.staffName, 
    features.featureID 
FROM 
    features 
    LEFT JOIN staff ON 
        features.staffID = staff.staffID 
WHERE features.postedOn != MAX(features.postedOn)
ORDER BY dummy DESC LIMIT 0,15

This query returns the following error:此查询返回以下错误:

MySQL error: #1111 - Invalid use of group function MySQL 错误:#1111 - 组 function 的使用无效

Is there any way around this?有没有办法解决?

The max query needs to be in its own subquery, so your final SQL should be:: max查询需要在自己的子查询中,因此最终的SQL应该是::

SELECT features.featureTitle AS title,
    features.featureSummary AS body, 
    features.postedOn AS dummy,
    DATE_FORMAT( features.postedOn,  '%M %d, %Y' ) AS posted,
    NULL,
    NULL,
    staff.staffName,
    features.featureID 
FROM 
    features 
    LEFT JOIN staff ON 
        features.staffID = staff.staffID
WHERE
   features.postedOn != (select max(features.postedOn) from features)

the problem you have is that is that you need to find the max (latest) feature from the table, while going over each row, but MAX() is a group function - you have to group all rows to use it. 你遇到的问题是你需要从表中找到最大(最新)功能,同时遍历每一行,但MAX()是一个组功能 - 你必须将所有行分组才能使用它。

you can use a sub-select to get the id of the last feature: 您可以使用子选择来获取最后一个功能的ID:

WHERE featureId <> (SELECT featureId From features ORDER BY postedOn DESC LIMIT1)

there is a problem with this approach - the subselect is run for every row, but it is not that expensive. 这种方法存在问题 - 每行都会运行子选择,但这并不昂贵。

You could also order by the PostedOn field in descending order and add OFFSET 1, which will display your results starting from the second row.您还可以按 PostedOn 字段降序排列并添加 OFFSET 1,这将从第二行开始显示您的结果。

SELECT features.featureTitle AS title,
       features.featureSummary AS body, 
       features.postedOn AS dummy,
       DATE_FORMAT(features.postedOn, '%M %d, %Y') AS posted,
       NULL,
       NULL,
       staff.staffName,
       features.featureID 
FROM 
       features 
       LEFT JOIN staff ON 
       features.staffID = staff.staffID
ORDER BY features.postedOn DESC
OFFSET 1;

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

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