常用数据库sql语句写法整理
--创建一个用户表
create table users (id int(10),pwd char(20),name varchar(10));
char表示占20个长度的空间 varchar根据实际长度决定空间大小
--复制表
create table users_copy as select * from users;
--增加数据到表中
方式1:insert into users values(1,‘1234’,‘d1’);
方式2:insert into users (id,name)values(2,‘d2’);
--查询数据
最简单的 select * from users;
条件查询 select * from users where id=1 ;
select * from users where id=1 and name=‘d1’ ;(and表示且的意思)
select * from users where id=1 or id=2 ;(or表示或的意思)
select * from users where id in(1,3,4);(or的条件多了,用in)
select * from users where id》=2;(可以有大小条件)
select count(*) from users;(查询有多少条数)
select * from employ where name like ‘%duan%’;(模糊查询)
select * from employ where name like ‘duan%’;(模糊查询,表示以duan开头---结尾反之)
select distinct name from users;(除去重复记录)
select * from employ order by id;(由小到大)
select * from employ order by id desc;(反之顺序排列)
查询的时候可以计算
select name,salary salary*12 from employ;(员工的年薪)
select name,salary+bonus from employ;(员工的月薪)
分组查询
select job, count(*)from emp_ning group by job;
过滤分组结果:having短语
select deptno, count(*)from emp_ning group by deptno having count(*) 》= 5;
子查询(即用某一查询语句代替语句中要写的数据)
select name from emp_ning where salary 》 (select salary
from emp_ning where id = 1002);
--子查询的结果集是多行多