在JOIN查询上使用ORDER BY进行排序

时间:2012-09-19 04:59:06

标签: mysql sql join sql-order-by

我正在尝试使用ORDER BY对查询进行排序,但它声称存在语法错误。在尝试排序之前,我的查询看起来像这样:

SELECT u.*, s.*
FROM bands u
inner join statuses s on u.status_id = s.id
WHERE u.status_id = 1
LIMIT {$startpoint}, {$limit}

我尝试过像这样添加ORDER BY,但似乎不正确:

SELECT u.*, s.*
FROM bands u
inner join statuses s on u.status_id = s.id
WHERE u.status_id = 1
LIMIT {$startpoint}, {$limit} ORDER BY u.status_id

如何在此查询中正确使用ORDER BY

2 个答案:

答案 0 :(得分:1)

ORDER BY子句出现在LIMIT

之前
SELECT u.*, s.*
FROM bands u
inner join statuses s on u.status_id = s.id
WHERE u.status_id = 1
ORDER BY u.status_id
LIMIT {$startpoint}, {$limit} 

答案 1 :(得分:1)

你几乎就在那里 - 你的命令和限制是错误的:

SELECT 
    u.*, s.* 
FROM 
    bands u inner join statuses s on u.status_id = s.id 
WHERE 
    u.status_id = 1 
ORDER BY u.status_id
LIMIT {$startpoint}, {$limit}