zoukankan      html  css  js  c++  java
  • mysql 合并查询 UNION

    语法

    SELECT expression1, expression2, ... expression_n FROM tables
    [WHERE conditions]
    UNION [ALL | DISTINCT]
    SELECT expression1, expression2, ... expression_n FROM tables
    [WHERE conditions];

    参数说明

    • expression1, expression2, ... expression_n: 要检索的列。

    • tables: 要检索的数据表。

    • WHERE conditions: 可选, 检索条件。

    • DISTINCT: 可选,删除结果集中重复的数据。默认情况下 UNION 操作符已经删除了重复数据,所以 DISTINCT 修饰符对结果没啥影响。

    • ALL: 可选,返回所有结果集,包含重复数据。

    实例演示

    一个登录日志表  按年分表 log_2018 log_2019 log_2020

    日志表字段 id name(登录人名字) ip(ip地址) desc (登录说明)delFlag(删除状态)  at(登录时间)

    查询2018-2020年所有的日志

    select id,name,ip,desc,delFlag,at from log_2020
    union all
    select id,name,ip,desc,delFlag,at from log_2019 
    union all
    select id,name,ip,desc,delFlag,at from log_2018 
    where delFlag = 0 order by at desc
  • 相关阅读:
    两条斜线
    Cantor表
    城市网络
    一起来数二叉树吧
    牛客网音乐研究(枚举)
    删括号
    合并回文子串
    寻找道路
    EXTJS 4.0.2 XML数据
    extjs4.0.2a gridpanel看不到横向滚动条的一种原因
  • 原文地址:https://www.cnblogs.com/rchao/p/13408116.html
Copyright © 2011-2022 走看看