包括查询中的排序

时间:2012-04-17 09:25:08

标签: php mysql sql sorting

我最近添加了一个排序字段,现在我想对结果进行排序。我可以用PHP或直接在数据库上完成。所以我尝试了第二个:

SELECT *
FROM constructions
AS whole
INNER JOIN
(
    SELECT DISTINCT construction
    FROM data
    AS results
    WHERE product =2
    AND application =1
    AND requirement =1
)
ON whole.id = results
ORDER BY whole.sorting

我尝试使用内部联接来匹配整个表和结果集。但我不能让它运作(#1248 - Every derived table must have its own alias)。我试图使用别名,但仍然有问题。也许我不应该使用内部联接而是使用IN()

2 个答案:

答案 0 :(得分:3)

语法中有两个明显的错误:

SELECT *
FROM constructions
AS whole
INNER JOIN
(
    SELECT DISTINCT construction AS results
    FROM data
    WHERE product =2
    AND application =1
    AND requirement =1
) AS a
ON whole.id = a.results
ORDER BY whole.sorting

答案 1 :(得分:1)

尝试获取此类数据......

 SELECT *
    FROM constructions
    where 
    construction
    in
    (
        SELECT DISTINCT construction
        FROM data
        AS results
        WHERE product =2
        AND application =1
        AND requirement =1
    )