• UNION ALL合表查询


    有时候需要连表查询数据,可以使用union all来做合表。

    语法:

    SELECT column_name FROM table1
    UNION ALL
    SELECT column_name FROM table2

    例如:

    $sql = "select rg.* from (
    SELECT g.goods_id, g.sort_order, g.goods_name, g.goods_number, g.suppliers_id, g.goods_name_style, g.market_price, g.shop_price ,
    g.promote_start_date, g.promote_end_date, g.goods_brief, g.goods_thumb , g.goods_img,g.little_img ,g.team_num,g.team_price
    FROM ". $GLOBALS['hhs']->table('goods') . " AS g ,".$GLOBALS['hhs']->table('suppliers')." AS s WHERE ".$where.
    " UNION ALL SELECT g.goods_id, g.sort_order, g.goods_name, g.goods_number, g.suppliers_id, g.goods_name_style, g.market_price, g.shop_price ,
    g.promote_start_date, g.promote_end_date, g.goods_brief, g.goods_thumb , g.goods_img,g.little_img ,g.team_num,g.team_price FROM ". $GLOBALS['hhs']->table('goods') ." AS g
    WHERE ".$main_where.") AS rg ORDER BY rg.`".$sort."` $order " . $limit;

    合表,一般常用的就是UNION ALL,也可以UNION,两者区别:
    1.Union All操作仅仅是简单的将两个子查询结果集直接求并操作,并不会剔除掉两者结果集中重复的部分,
    而Union操作除了会剔除掉结果集中重复的部分以外,还会对结果集进行排序。

    2.由于union执行是先将某一子结果集进行排序,然后再判断是否有重复的数据,若有则删除掉重复的数据,所以union的查询效率要低于union all。
  • 相关阅读:
    Linux
    Linus
    Linux
    Linux
    Linux
    Linux
    Linux
    Linux
    Django日志配置
    linux
  • 原文地址:https://www.cnblogs.com/xubinbk/p/6669110.html
Copyright © 2020-2023  润新知