mysql引用聚合表列名没有别名

时间:2012-08-07 11:04:37

标签: mysql

我做了

create table tmp select min(xxx_id) from xxx group by y having count(*)>1;

现在我需要将这个tmp表与另一个表连接起来,但是如何引用tmp表中唯一的列呢?

select * from table2 s, tmp t where s.xxx_id=t.xxx_id?

显然不起作用,应该替换t.xxx_id?

2 个答案:

答案 0 :(得分:0)

您需要将该列括在引号中:

select * from table2 s, tmp t where s.xxx_id = t.`min(xxx_id)`;

但为列提供别名总是更好,因为它不会让人感到困惑:

create table tmp select min(xxx_id) AS min_xxx_id from xxx group by y having count(*)>1;

答案 1 :(得分:0)

试试这个:

select * 
from table2 s join tmp t  
on s.xxx_id=t.xxx_id