【问题标题】:mysql reference aggregate table column name without aliasmysql引用聚合表列名没有别名
【发布时间】:2012-08-07 11:04:36
【问题描述】:

我做到了

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

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

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

显然行不通,应该用什么替换 t.xxx_id?

【问题讨论】:

    标签: mysql


    【解决方案1】:

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

    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;
    

    【讨论】:

      【解决方案2】:

      试试这个:

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

      【讨论】:

        猜你喜欢
        • 2010-12-19
        • 1970-01-01
        • 1970-01-01
        • 2010-09-27
        • 1970-01-01
        • 2019-05-30
        • 2022-07-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多