http://sqlfiddle.com/#!2/1a2df
我试图通过计算SUM(valid
)的MAX值来选择列“photo_name”
首选的结果是“test5.jpg”,但经过几个小时的尝试,我仍然无法弄明白,
下面是我之前的方法,但它不起作用
SELECT photo_name FROM
(
SELECT a.*
FROM test a
INNER JOIN
(
SELECT *, SUM(valid) v
FROM test
WHERE page_id = 3 AND `valid` = 1
) b ON MAX(b.v)
)c
请帮忙,
答案 0 :(得分:0)
这样的事情应该有效。它会为每张照片的valid
列求和,按总和从高到低排序,然后将结果限制在最上一行:
SELECT photo_name, SUM(valid) AS sum_valid
FROM test
GROUP BY photo_name
ORDER BY sum_valid DESC
LIMIT 1