我想在查询MySQL 中获得最大值,而没有MAX()函数,因为它非常慢。理想情况下,我想要LEFT JOIN
。
参考文献:
https://dev.mysql.com/doc/mysql-tutorial-excerpt/5.7/en/example-maximum-column-group-row.html
INNER JOIN Results from Select Statement using Doctrine QueryBuilder
我的查询MySQL:
SELECT
fs.id AS subject,
fpv.id AS post_version_id
FROM
forum_subject fs
INNER JOIN
forum_post_version fpv
ON fpv.forum_subject_id = fs.id
WHERE
fs.id IN (10817, 10818)
结果:
主题| post_version_id
10817 | 528385
10817 | 528386
10818 | 528387
10818 | 528388
我只想返回一行:最大值post_version_id
。
感谢您考虑我的实际情况要复杂得多:
编辑:原始查询:
SELECT
f13_.id
FROM
forum_subject_version f14_
LEFT JOIN forum_subject_version f15_ ON (
f15_.forum_category_id = f14_.forum_category_id
AND f14_.forum_subject_id = f15_.forum_subject_id
AND f14_.id < f15_.id
)
INNER JOIN forum_subject f16_ ON f14_.forum_subject_id = f16_.id
INNER JOIN forum_post_version f17_ ON (f17_.forum_subject_id = f16_.id)
LEFT JOIN forum_post_version f18_ ON (
f18_.forum_subject_id = f16_.id
AND f17_.forum_post_id = f18_.forum_post_id
AND f17_.id < f18_.id
)
INNER JOIN forum_post f13_ ON f17_.forum_post_id = f13_.id
WHERE
f14_.forum_category_id IN (217, 218)
AND f15_.id IS NULL
AND f18_.id IS NULL
返回4800行(#217 forum_category_id
中的2400行,#218 forum_category_id
中的2400行),我要2行,每个论坛类别(1行在#217 forum_category_id
中,1行中#218 forum_category_id
)。
我无法使用ORDER + LIMIT,并且我不想要这样:MIN/MAX vs ORDER BY and LIMIT。 MySQL建议使用LEFT JOIN进行过滤,但在我的情况下在f13_别名之后我不能这样做。 :)