123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- drop table if exists stu;
- CREATE TABLE stu (
- id int,
- name varchar(20),
- age int,
- sex varchar(5),
- address varchar(100),
- math double(5,2),
- english double(5,2),
- hire_date date
- );
- INSERT INTO stu(id,name,age,sex,address,math,english,hire_date)
- VALUES
- (1,'马运',55,'男','杭州',66,78,'1995-09-01'),
- (2,'马花疼',45,'女','深圳',98,87,'1998-09-01'),
- (3,'马斯克',55,'男','香港',56,77,'1999-09-02'),
- (4,'柳白',20,'女','湖南',76,65,'1997-09-05'),
- (5,'柳青',20,'男','湖南',86,NULL,'1998-09-01'),
- (6,'刘德花',57,'男','香港',99,99,'1998-09-01'),
- (7,'张学右',22,'女','香港',99,99,'1998-09-01'),
- (8,'德玛西亚',18,'男','南京',56,65,'1994-09-02');
- select name ,age from stu;
- select * from stu;
- select
- name ,
- age
- from stu;
- select
- math as 数学 , english as 英语
- from stu;
- select
- math 数学 , english 英语
- from stu s;
- select s.math ,s.english from stu s;
- select distinct address from stu;
- select * from stu;
- select * from stu where age > 20;
- select * from stu where age >= 20;
- select * from stu where age >= 20 and age <= 30 ;
- select * from stu where age between 20 and 30;
- select * from stu where hire_date >= '1998-09-01' and hire_date <= '1999-09-01' ;
- select * from stu where hire_date between '1998-09-01' and '1999-09-01' ;
- select * from stu where age = 18;
- select * from stu where age != 18;
- select * from stu where age <> 18;
- select * from stu where age = 18 or age = 20 or age = 22;
- select * from stu where age in ( 18,20,22 );
- select * from stu where english is null ;
- select * from stu where english is not null ;
- select * from stu where english <=> null ;
- select * from stu where name like '马%';
- select * from stu where name like '_花%';
- select * from stu where name like '%德%';
- select * from stu where age > all();
- select * from stu order by age;
- select * from stu order by age ASC ;
- select * from stu order by math DESC ;
- select * from stu order by math DESC , english ASC ;
- select count(id) from stu;
- select count(1) from stu;
- select count(english) from stu;
- SELECT MAX(math) FROM stu;
- SELECT MIN(math) from stu;
- SELECT SUM(math) from stu;
- SELECT AVG(math) from stu;
- select * from stu;
- select avg(math) , sex from stu group by sex;
- select avg(math),count(sex),sex from stu group by sex;
- select avg(math),count(sex),sex from stu where math > 70 group by sex;
- select
- avg(math),count(sex),sex
- from stu
- where
- math > 70
- group by
- sex
- having
- count(sex) > 2;
- select * from stu limit 0 , 3;
- select * from stu limit 0 , 3;
- select * from stu limit 3 , 3;
- select * from stu limit 6 , 3;
- Select * from stu;
- create table person1 (
- name varchar(20) not null
- );
- insert into person1(name) values (null);
- insert into person1(name) values ('kunkun');
- create table person2 (
- id int auto_increment unique ,
- name varchar(20) not null
- );
- insert into person2(id,name) values (1,'kunkun');
- insert into person2(id,name) values (2,'kunkun');
- insert into person2(name) values ('kunkun');
- create table person3 (
- id int auto_increment ,
- name varchar(20) not null,
- unique (id)
- );
- insert into person3(name) values ('kunkun');
- insert into person3(id,name) values (1,'kunkun');
- create table person4 (
- id int primary key auto_increment ,
- name varchar(20) not null
- );
- insert into person4(name) values ('kunkun');
- insert into person4(id,name) values (1,'kunkun');
- create table person5 (
- id int primary key auto_increment ,
- name varchar(20) not null,
- age int(3) default 0
- );
- insert into person5(name) values ('kunkun' );
- insert into person5(name,age) values ('kunkun',null );
- create table dept(
- id int auto_increment primary key ,
- name varchar(50) not null unique
- );
- create table emp(
- id int auto_increment primary key ,
- name varchar(50) not null unique,
- dept_id int ,
- foreign key fk_id (dept_id) references dept(id)
- );
- insert into dept( name ) values ('开发部门');
- insert into emp( name , dept_id ) values ('kunk',1);
- delete from dept where id = 2;
|