Sql_拾疑_整理_搜集

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

你可能感兴趣的:(sql)