where型子查询是内层查询的结果,作为外层sql的比较条件.
select goods_id, goods_name from goods where goods_id = (select max(goods_id) from goods);
内层查询为select max(goods_id) from goods
外层查询为select goods_id, goods_name from goods where goods_id
就是在把最大的goods_id的值赋给外层的goods_id
也可以用比较运算符‘in‘来做:
select goods_id, cat_id, goods_name from goods where goods_id in (select max(goods_id) from goods group by cat_id);
‘in‘的意思是在某集合之内,也就是说在以cat_id分组的类中最大的goods_id赋给外层的goods_id, 这样可以得出每个组的最大goods_id.
时间: 2024-12-23 06:50:18