个人技术分享

where 是用于筛选表中满足条件的行,不可以和聚类函数一起使用
having 是用于筛选满足条件的组 ,可与聚合函数一起使用
在这里插入图片描述
所以having语句中不能使用select中定义的名字

今天我写实验的时候发现在where语句中可以用select中取的别名
然后又发现一个问题

在做多表查询,或者查询的时候产生新的表的时候会出现这个错误:Every derived table must have its own alias(每一个派生出来的表都必须有一个自己的别名)。

派生表要取个名字

select c_name,c_id_card,ifnull(sum(profit),0) as total_amount
from client 
left join (
    select pro_c_id,pro_quantity*p_amount as profit
    from property,finances_product
    where p_id = pro_pif_id and pro_type = 1  -- 连接条件
    union all -- 组合一下
    select pro_c_id,pro_quantity*i_amount as profit
    from property,insurance
    where i_id = pro_pif_id and pro_type = 2
    union all
    select pro_c_id,pro_quantity*f_amount as profit
    from property,fund
    where f_id = pro_pif_id and pro_type = 3
) f on c_id = pro_c_id    -- 没有这个f不行
group by c_id
order by total_amount desc;