SQL(sql)語句大全(insert、update、delete、select)
阿新 • • 發佈:2020-07-16
SQL(sql)語句大全
一、mysql基本指令
show databases 顯示當前資料庫
use mysql 使用當前資料庫
show tables 顯示當前資料庫下的表
desc user 查看錶結構
create database cd1706 建立資料庫
二、建立表
create table if not exists person(
id int not null auto_increment,
name varchar(20),
age int,
primary key(id)
);
create table chengdu1706( id int not null auto_increment, stdno int, name varchar(12), age int, birthday date, salary int, word varchar(20), primary key(id) );
三、增刪改查
1、插入或新增資料 insert into
insert into userinfo (id,username,password) values (1,"zuozuo","root"); insert into userinfo (username,password) values ("mingming","abc123"); insert into userinfo values (3,"xiaohua","qwer12"); insert into article values (1,"mingming","abc","daydayup",now()); insert into article values (4,"zuozuomu","eat","go to eat huoguo",timestamp());//表示當前時間戳 now() timestamp() CURRENT_TIMESTAMP()
2. 修改 update set
update userinfo set password = root123" where username = "zuozuo"; mysql> update userinfo set username = "zuozuomu" where id = 1 and password = "root123"; update user set password = "abc123" where id > 3; //批量修改 update 1809_user set age = age + 10 where id = 4;
3. 刪除 資料
delete from userinfo;
delete from userinfo where id = 1;
drop table userinfo; 刪表 //慎用
drop database cd1706 刪除資料庫 //慎用
truncate table userinfo 清空資料表 //慎用
4. 查詢 select
select * from userinfo;
select username ,password from userinfo;
select * from article order by author; //排序 預設升序
select * from article order by id desc; //排序 降序 desc 表示降序
select * from article order by author desc,content desc; author //降序 content 降序
select * from person order by age desc limit 3,2;
select * from article limit 3; //限制3條資料
select * from article where author = "mingming" order by enterdate limit 1;
select * from userInfo where id > 2 limit 2;
select * from userInfo limit 1,2;
select count(*),author from article group by author; //分組查詢
select min(enterdate),author from article group by author;
select max(salary),department from emp group by department;
select avg(salary),department from emp group by department;
select * from emp where salary in (select max(salary) from emp group by department);
select * from emp where age in (select max(age) from emp group by department);
select * from emp where salary in (10000,12000); 或者
select * from emp where age in (22,28);
select * from emp where age = 22 or age = 28;
select * from emp where age > 20 and age < 26;
select * from person where age < 20 or age > 30;
5、關聯表查詢
select * from emp , userinfo where emp.name = userinfo.username;
select * from emp e,userinfo u where e.name = u.username;
select * from article a ,emp e where e.name = a.author and e.age > 24 and a.id<5;
select * from article where author = (select name from emp order by age desc limit 1);
6、模糊查詢 like %%
select * from article where author like "ming%"; //開頭
select * from article where author like "%zuo%"; // 包含
select * from article where author like "%hua"; //結束