SQL命令--合并查询union
2024-10-12 10:36:58
union命令作用:连接两个以上的 SELECT 语句的结果组合到一个表中
一. 示例:
select id, url, auth_status as authStatus, enterprise_name as enterpriseName, establish_time as establishTime, 'node' as type, null as messageId, null as sender, created_time from node where node_role != 'SELF'
union
select id , null as url, null as authStatus, null as enterpriseName, null as establishTime, 'message' as type, id as messageId, sender,created_time from message where `message_type`='INVITE' and handled=0 order by created_time desc
注意事项:
1. 两表字段长度需要一致
2. order by 排序的字段,需要在查询字段中包含
3. union 合并时默认去重,若不需要去重,可以使用union all
二.获取链表查询的数量总和
select sum(a.b) as num from (
select count() as b from table_1
union all
select count() as b from table_2
) as a(注意这里要取个别名)
最新文章
- Centos 7防火墙firewalld开放80端口(转)
- Markdown编辑器简单总结
- MySQL模拟:线上误update的恢复
- SQL Server xtype
- HDU 4897 Little Devil I(树链剖分)(2014 Multi-University Training Contest 4)
- 544B. Sea and Islands
- 1.5.5 Tokenizers
- jar包冲突解决方法
- Android 签名(2)签名知识要点
- python中的字典(dict),列表(list),元组(tuple)
- 对c#剪切板Clipboard占用的问题一点解决方法
- Linux下find命令具体解释
- uva 10635 Prince and Princess(LCS成问题LIS问题O(nlogn))
- 三分钟浅谈TT猫的前端优化
- zzuli 2130: hipercijevi 链式前向星+BFS+输入输出外挂
- Redis-04.备份与恢复
- drf开发中常见问题
- layui loading
- JAVA核心技术I---JAVA基础知识(时间类)
- excel vba获取拼音