create table tab_b(
b_id int,
b_name varchar(20),
b_date date
)
create table tab_y(
y_id int,
y_xing varchar(20),
y_ming varchar(20),
y_date date,
y_phone varchar(20),
y_age int,
y_sex varchar(20),
b_id int
)
alter table tab_b add primary key(b_id);
alter table tab_b modify b_id int auto_increment;
insert into tab_b(b_name,b_date) values('股东部','1998-1-14');
select *from tab_b
alter table tab_y add primary key(y_id);
alter table tab_y modify y_id int auto_increment;
insert into tab_y(y_xing,y_ming,y_date,y_phone,y_age,y_sex,b_id) values('师','敏','1999-5-5','15896587452',20,'女',4);
select *from tab_y
insert into tab_y(y_xing,y_ming,y_date,y_phone,y_age,y_sex,b_id) values('师','敏','1999-5-5','15896587452',20,'女',4);
insert into tab_y(y_xing,y_ming,y_date,y_phone,y_age,y_sex,b_id) values
('小','敏','1999-8-15','15896857452',20,'女',6),('刘','航','1999-8-5','15878587452',20,'女',5),
('刘','航','1999-8-5','15878587452',20,'女',5);
delete from tab_y
delete from tab_y where y_id=12;
delete from tab_y where y_id in (13,14);
truncate table tab_y
update tab_y set y_ming='m敏' where y_id=10
update tab_y set y_ming='min敏' where y_id in(9,10);
select y_xing,y_ming,y_age,y_sex from tab_y where b_id=1
select y_xing as '姓',y_ming as '名',y_age as '年龄',y_sex as '性别' from tab_y
select concat(y_xing,y_ming) from tab_y limit 1,5
select *from tab_y order by y_id desc limit 3
select distinct y_id from tab_y;
select *from tab_y where y_age>20 order by y_age desc
select *from tab_y where y_age=20 and y_sex='女'
select *from tab_y where y_age>22 or b_id<>2
select *from tab_y where b_id != 1
select *from tab_y order by y_age asc
select *from tab_y order by y_age desc,y_date asc
select * from tab_b b,tab_y y where b.b_id=y.b_id
concat()拼接字符串
select concat(y_xing,y_ming) as '姓名' from tab_y
select concat('a','b') from dual
select length('你好啊') from dual
select length(y_xing) from tab_y
select char_length('asdf你') from dual
select upper('fghHHHjj') from dual
select lower('FGHfghFGH') from dual
select ltrim(' asd d') from dual
select rtrim(' as d ') from dual
select replace('aaabbbccc','a','f') from dual
select locate('z','cvbxzsd') from dual
select substring('asdfghjk',3,4) from dual
select now() from dual
select timestampdiff(year,'1997-03-14','1998-03-14') from dual
- date_Add()加日期{second 秒, minute 分钟,hour 小时,day 天,week 周,month 月,quarter 季,year 年}
select date_Add('1997-03-14',interval 1 day) from dual
select dayofweek('2019-09-5') from dual
select yearweek('2019-12-30') from dual
select datediff('1998-03-14','1997-03-14') from dual
select date('1997-03-14 00:00:12') from dual
SELECT concat(substring(title,1,10) , '...') AS '新闻标题'
,Clicks as 点击率 FROM NewsInfo
WHERE yearweek(date_format(AddTime,'%Y-%m-%d')) = yearweek(now())
ORDER BY Clicks desc limit 3;
abs()求绝对值
select abs(-12) from dual
select ceiling(20.1) from dual
select floor(12.9) from dual
select power(2,3) from dual
select round(12455.1235,2) from dual
select sign(-4) from dual
select sqrt(16) from dual
select pi() from dual
select rand() from dual
convert()类型转换
select convert(452,char(3)) from dual
select version() from dual
select last_insert_id() from dual
select y_age, IFNULL(y_age,y_id) y_phone from tab_y;
select user,host from mysql.user
select *from tab_y where y_xing like '白%'
select *from tab_y where y_phone like '%9%'
select *from tab_y where y_ming like '%武'
select *from tab_b
select *from tab_b where b_name like '技__'
select *from tab_b where b_name like '__部'
select *from tab_b where b_name like '_传%'
insert into tab_b(b_name,b_date) values('财_武部','1998-03-17')
select *from tab_b where b_name like '财/_%' escape '/'
select *from tab_y
select *from tab_y where y_phone regexp '^[1][8][1-7]'
select *from tab_y where y_age between 20 and 24
select *from tab_y where y_age between 20 and 24
select *from tab_b where b_name in ('销售部','技术部')
select AVG(y_age) from tab_y
create table tab_grade(
g_id int,
g_name varchar(20)
)
alter table tab_grade add primary key(g_id);
alter table tab_grade modify g_id int auto_increment;
insert into tab_grade(g_name) values('四年级')
select *from tab_grade
create table tab_class(
c_id int,
c_name varchar(20)
)
alter table tab_class add primary key(c_id)
alter table tab_class modify c_id int auto_increment
insert into tab_class(c_name) values('一班'),('二班'),('三班'),('四班')
select *from tab_class
create table tab_student(
s_id int,
s_name varchar(20),
s_sex varchar(20),
s_age int,
s_phone varchar(20),
s_idNumber varchar(18),
s_performance double(5,2),
s_date date,
g_id int,
c_id int
)
alter table tab_student add primary key(s_id)
alter table tab_student modify s_id int auto_increment
insert into tab_student(s_name,s_sex,s_age,s_phone,s_idNumber,s_performance,s_date,g_id,c_id)
values('貂蝉','女',21,'18597978965','411588144569874565',80.22,'1997-02-4',2,2),
('孙尚香','女',20,'18796488965','411527144569878542',89.49,'1997-07-12',2,2),
('大桥','女',20,'18796488965','411527144569878542',79.99,'1997-07-12',3,1),
('小乔','女',20,'18796488965','411527144569878542',89.49,'1997-07-12',4,1),
('虞夫人','女',20,'18796488965','411527144569878542',94.19,'1997-07-12',4,1)
SELECT g_id,AVG(s_performance) FROM tab_student GROUP BY g_id;
select c_id as '年级',count(c_id) from tab_student GROUP BY c_id
select g_id,count(g_id) from tab_student GROUP BY g_id
select c_id,min(s_age) from tab_student GROUP BY c_id ORDER BY s_age
update from tab_student set s_name='bai' where s_id=18
select *from tab_student
select c_id,count(s_sex),s_sex from tab_student GROUP BY c_id,s_sex
select g_id,min(s_age),s_name,count(g_id) from tab_student GROUP BY g_id having count(g_id)>2 ORDER BY MIN(s_age)
select g_id,avg(s_performance),count(*) from tab_student
where s_performance>=60 GROUP BY g_id HAVING avg(s_performance)>95
select *from tab_student
select s.s_id,s.s_name,s.s_age,s.s_sex,c.c_name from tab_student s,tab_class c where s.c_id=c.c_id
select s.s_id,s.s_name,s.s_age,s.s_sex,c.c_name from tab_student s INNER JOIN tab_class c on s.c_id=c.c_id
select * from tab_student s LEFT JOIN tab_class c on s.c_id=c.c_id
select * from tab_student s RIGHT JOIN tab_class c on s.c_id=c.c_id
select *from tab_student,tab_class;
select s.id, s.productname, p.productname from product s join product p on s.parent_id = p.id;