0

我有以下内容:

SELECT 
table1.id
FROM
table1 
LEFT OUTER JOIN table2 ON table2.table1_id = table1.id
WHERE
(table1.entry_id=2) 
AND 
parent_id=0 
ORDER BY 
SUM(table2.column1) - SUM(table2.column2)

在我添加“order by”之前工作正常,我需要它从 table1 中获取所有相关行,即使它们在 table2 中有不匹配的行,排序会将它们放在底部。

4

1 回答 1

0

试试这个:

SELECT 
  t1.id
FROM table1 AS t1
LEFT OUTER JOIN
(
   SELECT table1_id, SUM(column1) sum1, SUM(column2) sum2
   FROM table2 
   GROUP BY table1_id
) AS t2  ON t2.table1_id = t.id
        AND t1.entry_id  = 2 
        AND t1.parent_id = 0 
ORDER BY t2.sum1 - t2.sum2;
于 2013-04-05T09:20:50.940 回答