different
different

Reputation: 2383

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:

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

Is there any way around this?

Upvotes: 7

Views: 19815

Answers (3)

ola
ola

Reputation: 77

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.

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;

Upvotes: 0

Guss
Guss

Reputation: 32404

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.

you can use a sub-select to get the id of the last feature:

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.

Upvotes: 0

Eric
Eric

Reputation: 95233

The max query needs to be in its own subquery, so your final SQL should be::

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)

Upvotes: 6

Related Questions