2021-06-01

-- 創(chuàng)建數(shù)據(jù)庫

create database if not   exists school default charset = utf8

;
-- 使用數(shù)據(jù)庫

use school;
-- 創(chuàng)建表格
create table if not exists 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,"北平"),
("林黛玉","女",17,"浙江");    

-- 查看數(shù)據(jù) 通配符* 表示所有
select * from studentinfo;

-- 手動寫出字段 全量查詢
select id ,`name `,sex ,age ,address from studentinfo;

-- 只查詢指定的數(shù)據(jù)列
select id ,`name`,sex, from studentinfo;

-- 條件查詢 
select * from studentinfo where sex ='女';

-- 根據(jù)查詢結(jié)果排序 order by 
select * from studentinfo order by age desc ;
select * from studentinfo order by age desc , id desc ;

-- 顯示查詢結(jié)果的條數(shù)
select * from studentinfo limit 3 ;
-- 顯示查詢結(jié)果中從索引為5 開始的往后3條數(shù)據(jù)
select * from studentinfo limit 5, 3 ;

-- 復(fù)合
select * from studentinfo where sex = '女' order by age desc limit 3 ;
-- 別名 As as 可以省略
select id as 編號 , `name `as 姓名,sex as 性別,age  as 年齡 , address as 地址, from studentinfo;
select id 編號, `name ` 姓名 ,sex 性別,age 年齡, address 地址, from 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 = '女';
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容