1.增
use myblog;
show tables;
insert into users(username, `password`, realname) values ('zhangsan', '123', '張三');
2.查
use myblogs;
show tables;
select * from users; --查詢(xún)所有
select id,username form users; --查詢(xún)id和username
select * from users where username='zhangsan'; --條件查詢(xún)
select * from users where username='zhangsan' and `password`='123'; --并且
select * from users where username='zhangsan' or `password`='123'; --或者
select * from users where username like '%zhang%'; --模糊查詢(xún)
select * from users where password like '%1%' order by id desc; --desc表示倒序俗他,不加默認(rèn)正序
select * form users where state <> '0'; -- <>表示不等于0
select count(id) as `count` from blogs; -- 查詢(xún)總數(shù)
select * from blogs order by id desc limit 2; -- 查詢(xún)第一頁(yè)的兩條數(shù)據(jù)
select * from blogs order by id desc limit 2 offset 2; -- 查詢(xún)第二頁(yè)的兩條數(shù)據(jù)
3.改(更新)
如果遇到update users set realname...報(bào)錯(cuò)的話(huà)中姜,先執(zhí)行 SET SQL_SAFE_UPDATES = 0; 然后刪掉再執(zhí)行更新操作
update users set realname='李四2' where username='lisi';
4.刪
delete from users where username='lisi';
-- 日常開(kāi)發(fā)中我們通常是采用軟刪除
update users set state='0' where username='lisi';
5. 多表聯(lián)查
select blogs.*, users.username, users.nickname
from blogs inner join users on users.id = blogs.userid
where users.username = 'lisi'