0

我有一个包含许多表的数据库。每个表只存储了 ID。现在我想做的是:

SELECT id FROM table1, table2, table3
GROUP BY id;

但我也想按出现的递减顺序对它们进行排序。

例如,所有 3 个表中的 ID 应该出现在顶部,而只出现在一个表中的 ID 应该出现在底部。关于如何做到这一点的任何线索?

4

3 回答 3

5

也试试这个

select id from
(
    SELECT id FROM table1
    union all
    select id from table2
    union all
    select id from table3 
) as t
GROUP BY id
order by count(id) desc
于 2012-07-17T12:27:51.563 回答
1
select sum(t1.id IS NOT NULL,t2.id IS NOT NULL, t3.id IS NOT NULL) as total,t1.id from table1 as t1 join table2 as t2 on t1.id=t2.id join table3 as t3 on t3.id = t1.id order by total desc;

我不确定您的问题,但这可以提供帮助

于 2012-07-17T12:12:20.993 回答
-2
select id from
(
    SELECT id FROM table1
    union all
    select id from table2
    union all
    select id from table3 
) as t
GROUP BY id
order by id desc
于 2012-07-17T12:00:46.743 回答