我想在我的查询中设置count(t.name)
来计算行数,但是一旦我添加count(t.name)
order by
被忽略 - 我仍然会得到结果和正确的行数,但顺序错误。
SELECT t.name, t.team_id, count(t.name) as count
FROM st_team t INNER JOIN st_position p
ON t.team_id = p.team_id
WHERE p.league_id = '$league_id'
AND p.type = 'full'
AND p.grid = '0'
ORDER BY t.name
LIMIT 0,1
我认为它必须与't'有关。前缀,但尝试各种组合并没有让我在任何地方。
任何帮助非常感谢,欢呼P
答案 0 :(得分:0)
如果你需要Count,你需要Group by:
AND p.grid = '0'
GROUP BY t.name
ORDER BY t.name
LIMIT 0,1
答案 1 :(得分:0)
由于您使用的是摘要功能(COUNT
),因此查询中需要GROUP BY
语句。
SELECT
t.name,
t.team_id,
COUNT(t.name) as count
FROM
st_team t
INNER JOIN
st_position p USING(team_id)
WHERE
p.league_id = '$league_id'
AND p.type = 'full'
AND p.grid = '0'
GROUP BY 1, 2
ORDER BY 1 --This orders by t.name in ascending order. Change to 3 DESC to order by count descending
LIMIT 0,1;