-- 創(chuàng)建數(shù)據(jù)庫
create database if not exists school default charset=utf8;
-- 使用數(shù)據(jù)庫
use school;
-- 創(chuàng)建表格
creat table studentinfo (
id int primary key auto_increment ,
name
varchar(20) ,
sex char ,
age int ,
address varchar(100)
);
-- 查看數(shù)據(jù)表
show tables;
-- 插入數(shù)據(jù)
insert into studentinfo (name
,sex,age , address) values ("賈寶玉","男"舞终,18伙判,"北平"),
("賈政", "男", 40 ,"浙江")迹鹅,("晴雯"担孔,"女"江锨,20,"四川"),("襲人","女",29,"貴州"),
("薛寶釵","女",19,"北平")糕篇,("林黛玉"啄育,"女",19拌消,"浙江")挑豌;
-- 查看數(shù)據(jù) 通配符 * 表示所有
select * from studentinfo;
-- 手動(dòng)寫出字段 全量查詢
select id, name
,sex , age ,address from studentinfo;
-- 只查詢指定的數(shù)據(jù)列
select id, name
,sex from studentinfo;
-- 條件查詢
selecct * from studentinfo where sex='女';
-- 根據(jù)查詢結(jié)果排序 order by
select * grom studentinfo order by age aesc;
-- 顯示查詢結(jié)果的條數(shù)
select * from studentinfo limit 3 ;
-- 復(fù)合
select * from studentinfo where sex='女' order by age desc limit 3;
-- 別名 AS as 可以省略
select id as 編號(hào) ,name
as 姓名安券,sex as 性別 ,age as 年齡氓英, address as 地址 from studentinfo侯勉;
select id 編號(hào),name
姓名铝阐, sex 性別址貌,age 年齡 ,address 地址 studentinfo;
-- 單條件查詢
select * from studentinfo where age<20;
select * from studentinfo where age is not null;
-- 多條件查詢
-- 不等于
select * from studentinfo where age!=20;
select * from studentinfo where age<>20;
select * from studentinfo where not (age=20);
-- 與 或
select * from studentinfo where age >20 && sex='女';
select * from studentinfo where age >20 and sex='女';
select * from studentinfo where age >20 || sex='女';
select * from studentinfo where age >20 or sex='女';
-- 去重 去除重復(fù)的數(shù)據(jù)
select distinct name
,sex,age,address from studentinfo;