MYSQL是目前用的,如有变化,会说明。
#Union的用法及好处
select id from deals where id = 7 union select id from deals where id < 5 order by id;
#1 可以看出来,union之后居然可以再排序等操作
#2 union的column最好是一样的
#3 union的好处是开始会作为两条sql处理,在or导致不能用索引时,比较常用。
#4 union也是默认左边优先的,见下面的sql,结果的column优先用左边的
mysql> select id+10 id, id+1 id2 from deals where id between 1 and 5;
+----+-----+
| id | id2 |
+----+-----+
| 11 | 2 |
| 12 | 3 |
| 13 | 4 |
| 14 | 5 |
| 15 | 6 |
+----+-----+
5 rows in set (0.00 sec)
mysql> select id+10 id, id+1 id3 from deals where id between 4 and 8;
+----+-----+
| id | id3 |
+----+-----+
| 14 | 5 |
| 15 | 6 |
| 16 | 7 |
| 17 | 8 |
| 18 | 9 |
+----+-----+
5 rows in set (0.00 sec)
mysql> select id+10 id, id+1 id2 from deals where id between 1 and 5 union select id+10 id, id+1 id3 from deals where id between 4 and 8;
+----+-----+
| id | id2 |
+----+-----+
| 11 | 2 |
| 12 | 3 |
| 13 | 4 |
| 14 | 5 |
| 15 | 6 |
| 16 | 7 |
| 17 | 8 |
| 18 | 9 |
+----+-----+
8 rows in set (0.00 sec)
#in语句的区别和细节
#exist语句和细节
这个可以看我的博文:对exists的总结:http://fantaxy025025.iteye.com/blog/1146776
看之前可以考考你自己哦:
--users表有1000条记录,id自增,id都大于0
select * from users where exists (select * from users limit 0); --输出多少条记录?
select * from users where exists (select * from users where id < 0); --输出多少条记录?
答案(请选中查看):
10000条
0条
#想想想
#想想想
#想想想
X
X
X
-
X
X
X
分享到:
相关推荐
Registar_LECCO_SQL_Expert_For_SQL_Server_v25.rar Registar_LECCO_SQL_Expert_For_SQL_Server_v25.rar
HeidiSQL_12.0_64_Portable数据库连接软件 HeidiSQL_12.0_64_Portable数据库连接软件 HeidiSQL_12.0_64_Portable数据库连接软件
SQL_函数SQL_函数SQL_函数SQL_函数SQL_函数SQL_函数SQL_函数SQL_函数SQL_函数
cn_sql_server_2008_r2_express_x64
oracle sqlt工具脚本,用来使用sql_profile绑定内存中已有的执行计划
SqlServer2012安装包 cn_sql_server_2012_enterprise_edition_with_sp1_x64_dvd_1234495.iso
cn_sql_server_2012_express_management_studio_x64 数据库管理工具
coe_xfr_sql_profile迁移统计信息 coe_xfr_sql_profile迁移统计信息 coe_xfr_sql_profile迁移统计信息 coe_xfr_sql_profile迁移统计信息
cn_sql_server_2014_enterprise_edition_x64_dvd_3932882.iso,数据库安装镜像
cn_sql_server_2012_enterprise_edition_with_sp1_x64_dvd_1234495.iso sqlserver2012完整版数据库下载
cn_sql_server_2008_r2_express_with_management_tools_x64和x86版本
讲解SQL_Server_2005教程,SQL_Server_2005教程.doc SQL_Server_2005教程.doc SQL_Server_2005教程.doc SQL_Server_2005教程.doc SQL_Server_2005教程.doc SQL_Server_2005教程.doc SQL_Server_2005教程.doc SQL_...
cs_sql_2005_dev_all_dvd X86 开发版 安装和学习,调试用的数据库。 文件小,安装快,功能强大 。
C#例子代码 A0161_SQL_Server_Porc_UnitTestC#例子代码 A0161_SQL_Server_Porc_UnitTestC#例子代码 A0161_SQL_Server_Porc_UnitTestC#例子代码 A0161_SQL_Server_Porc_UnitTestC#例子代码 A0161_SQL_Server_Porc_...
《MySQL开发者SQL权威指南》的例子代码
C_net_sql_c#_pdf阅读器笔试题.rarC_net_sql_c#_pdf阅读器笔试题.rarC_net_sql_c#_pdf阅读器笔试题.rarC_net_sql_c#_pdf阅读器笔试题.rarC_net_sql_c#_pdf阅读器笔试题.rarC_net_sql_c#_pdf阅读器笔试题.rarC_...
C#例子代码 A0160_SQL_ServerC#例子代码 A0160_SQL_ServerC#例子代码 A0160_SQL_ServerC#例子代码 A0160_SQL_ServerC#例子代码 A0160_SQL_ServerC#例子代码 A0160_SQL_ServerC#例子代码 A0160_SQL_ServerC#例子代码 ...
coe_xfr_sql_profile.sql
C#例子代码 A0162_SQL_Server_Func_MyRule_UnitTestC#例子代码 A0162_SQL_Server_Func_MyRule_UnitTestC#例子代码 A0162_SQL_Server_Func_MyRule_UnitTestC#例子代码 A0162_SQL_Server_Func_MyRule_UnitTestC#例子...