SQL-запрос для выбора всего кроме значения Max

Один пример - когда у Вас есть зависимости между объектами... т.е.: Документ-> DocumentItems (при удалении Документа DocumentItems не имеют причины существовать)

6
задан OMG Ponies 26 April 2011 в 23:28
поделиться

2 ответа

Запрос 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)
6
ответ дан 17 December 2019 в 02:32
поделиться

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.

0
ответ дан 17 December 2019 в 02:32
поделиться
Другие вопросы по тегам:

Похожие вопросы: