1. select語句
通過 * 把 users 表中所有的數據查詢出來
select * from users
從 users 表中把 username 和 password 對應的數據查詢出來
select username, password from users
2. INSERT INTO語句
向 users 表中,插入新數據,username 的值為 tony stark password 的值為 098123
insert into users (username, password) values ('super_idol', '098123')
3. UPDATE 語句
將 id 為 4 的用戶密碼,更新成 888888
update users set password='888888' where id=4
更新 id 為 2 的用戶,把用戶密碼更新為 admin123 同時,把用戶的狀態(tài)更新為 1
update users set password='admin123', status=1 where id=2
4.DELETE 刪除語句
刪除 users 表中, id 為 4 的用戶
delete from users where id=4
5. 演示 where 子句的使用
select * from users where status=1 // 查詢表中狀態(tài)為1的用戶
select * from users where id>=2 // 查詢表中id大于等于2的用戶
select * from users where username<>'ls' // 查詢表中不等于'ls'的用戶
select * from users where username!='ls' // 查詢表中不等于'ls'的用戶
6. AND 和 OR 運算符
使用 AND 來顯示所有狀態(tài)為0且id小于3的用戶
select * from users where status=0 and id<3
使用 or 來顯示所有狀態(tài)為1 或 username 為 zs 的用戶
select * from users where status=1 or username='zs'
7. ORDER BY 排序語句
對users表中的數據,按照 status 字段進行升序排序
select * from users order by status
按照 id 對結果進行降序的排序 desc 表示降序排序 asc 表示升序排序(默認情況下,就是升序排序的)
select * from users order by id desc
對 users 表中的數據,先按照 status 進行降序排序,再按照 username 字母的順序,進行升序的排序
select * from users order by status desc, username asc
8. COUNT(*) 函數
使用 count(*) 來統(tǒng)計 users 表中,狀態(tài)為 0 用戶的總數量
select count(*) from users where status=0
使用 AS 關鍵字給列起別名
select count(*) as total from users where status=0
select username as uname, password as upwd from users
9. 分頁查詢 LIMIT()
分頁查詢公式:(currentPage - 1) * pagesize
-- ( 當前頁 - 1 ) * 每頁顯示條數
-- (1-1) * 2 =0
-- (2-1) * 2 =2
-- (3-1) * 2 =4
LIMIT 參數1,參數2 參數1=從哪開始取值 參數2=你要取幾個
select * from ev_users limit 4,2
10. 多表查詢 左連接
SELECT art.`Id`,art.`title`,art.`pub_date`,art.`state`,cate.`name`
FROM ev_articles art LEFT JOIN ev_article_cate cate
ON art.`cate_id` = cate.`Id` LIMIT 2,2