MySQL where型子查询

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.

    

    

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。