SQL語句的使用

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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容