phoenix13

导航

 

1.模糊查询

SELECT * FROM mytable WHERE u_name LIKE '%01%'

 

2.创建视图

create view stu_mj
as
select student.NO,student.NAME,student.SEX,student.BIRTHDAY,student.MEMBER,major.MAJOR
from student,major
where student.MAJOR_KEY=major.MAJOR_KEY

 

 

3. 为table添加备注

alter table 表名 comment '注释';

 

4. 结果排序

select *
from z_users 
order by followers_count desc   #desc 从大到小 asc 从小到大

 

5.注释

我试了下mysql只能用“#”或‘/*  */’。

不能用‘--’,要出错。

http://zhidao.baidu.com/question/174428030.html

 

6. BETWEEN

SELECT * FROM Persons
WHERE LastName
BETWEEN 'Adams' AND 'Carter'

 

7. IN 

in比连写几个or方便,而且执行效率更高

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)

http://www.w3school.com.cn/sql/sql_in.asp  SQL IN 操作符

 

8. group by 分组

select province, count(*) from users 
group by province                    #按照province来进行统计

9. having

于group by 一起用,取代where,作用和where相同。having支持所有where 的操作符。

select province, count(*) from users 
group by province
having count(*)<100

 

10. select子句顺序

(1) select

(2) from

(3) where

(4) group by

(5) having

(6) order by

(7) limit

 

11. inner join,  left join,  right join

left join: 对于left表,即使没有匹配也会列出。

right join: 对于right表,即使没有匹配也会列出。

http://www.jb51.net/article/15386.htm  超详细mysql left join,right join,inner join用法分析

 

inner join和where用法差不多。

WHERE子句中使用的连接语句,在数据库语言中,被称为隐性连接。INNER JOIN……ON子句产生的连接称为显性连接。

WHERE 和INNER JOIN产生的连接关系,没有本质区别,结果也一样。但是!隐性连接随着数据库语言的规范和发展,已经逐渐被淘汰,比较新的数据库语言基本上已经抛弃了隐性连接,全部采用显性连接了。

 

http://blog.csdn.net/qingtanlang/article/details/2133816  inner join 和where 区别

 

12. 自连接和子查询

自连接和子查询基本上可以互换,但是自连接在效率上可能要高。

例子:某物品(ID为DT)可能存在问题,因此想要知道生产该物品的供应商生产的其他物品是否也有问题。查询要求首先找到ID为DTNTR的物品的供应商,然后找出这个供应商的全部物品。

子查询

select prod_id, prod_name
from products
where vend_id = ( select vend_id 
                from products 
                where prod_id = 'DT')

 

自连接

select prod_id, prod_name
from products as p1, products as p2
where p1.vend_id=p2.vend_id
    and p2.prod_id='DT'

 

13. union 

连接2条或多条select语句。

union重复值只选取1条,要显示全部重复值用union all

SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2

http://www.w3school.com.cn/sql/sql_union.asp  SQL UNION 和 UNION ALL 操作符

 

14. delete

删除行

delete from users
where id=1000

 

15. 触发器 trigger

一个表最多只有6个触发器: insert,update,delete之前和之后。

create trigger neworder after insert on orders
for each row select NEW.order_num

新表用NEW关键字,旧表用OLD关键字。

 

16. 管理事物处理 commit, rollback,savepoint。

start transaction;
insert into xxx
values(6,'asdas');
commit

事务一般都是自动提交的,也就是说单写insert,delete就可以了。但是要是写了start transaction,事务就不会自动提交,要等到commit命令才会提交。

start transaction;
insert into xxx
values(6,'asdas');
rollback

使用savepoint,rollback只到savepoint的位置。

start transaction;
insert into xxx
values(8,'asdas');
savepoint d1;
insert into xxx
values(9,'asdas');
rollback to d1;
commit;

 

17. 用户管理

查看用户

use mysql;
select user from user

创建用户

create user ben identified by '123'

查看权限

show grants for ben

赋予权限

grant select on table1 to ben

 

18. where exists用法

例子:找寻和某用户location相同的用户,通过自查询

exists返回真假 

select * from tag_users as a
where exists(select 1 from tag_users where  name='不胖大海' and location=a.location)

 http://www.cnblogs.com/diction/archive/2008/01/18/1043844.html    in和exists的区别与SQL执行效率分析

例子:从users选择tmp表中没有的id

select * from users  as t
where not exists
(select 1 from tmp  as a where a.id=t.id )

用in来执行相同的查询

select * from users  as t
where t.id not in 
(select id from tmp)

 

posted on 2012-12-10 15:49  phoenix13  阅读(427)  评论(0编辑  收藏  举报