美文网首页
PHP -- 数据库8 -- 连接查询 子查询

PHP -- 数据库8 -- 连接查询 子查询

作者: 潘肚饿兵哥哥 | 来源:发表于2019-08-26 22:11 被阅读0次
    <?php
    //header("Content-type:text/html;charset=utf-8");
    
     
     连接查询:
        左连接:主表在左边  左右连接可以相互转换
            a left join b on 连接条件  == b  right join a 左右连接可以相互转换
        右连接:主表在右边
            a right join b on 连接条件
        内连接(只会显示交集部分,没有交集的数据会被丢弃)
            a [inner] join b on 连接条件
        
            
     
     子查询:
        where型:
        from型:
        
    
    查询出最高价格的商品信息(商品id,名称,本店价格)
    select后面最好是跟具体的列,而不是用*,因为如果两张表有重复命名的字段,就会报错
    
        1.查询最高的价格 select max(shop_price) from goods;
            select goods_id, goods_name, shop_price from goods where shop_price=(select max(shop_price) from goods);
            
        查询每个栏目中价格最高的商品信息(商品id,名称,本店价格)5999 4999 3999
        
        //select goods_id, goods_name, shop_price from goods where shop_price in (5999, 4999, 3999)
        
        更好的写法:
        select max(shop_price) from goods group by cat_id;//先拿到每个栏目的最高价格
        select goods_id, goods_name, shop_price cat_id from goods where shop_price in (select max(shop_price) from goods group by cat_id);
        
        
        
        
        用where型子查询把goods表中的每个栏目下面最新的商品取出来
        select goods_id, goods_name, shop_price cat_id from goods where goods_id in (select max(goods_id) from goods group by cat_id);
        
        
     
    
    ?>
    
    

    相关文章

      网友评论

          本文标题:PHP -- 数据库8 -- 连接查询 子查询

          本文链接:https://www.haomeiwen.com/subject/xkeysctx.html