SQL 語句(原生)


//查

 

//查詢表里的所有數據

select * from 表名   

 

//根據id等字段查詢數據

select * from 表名 where 字段 = 值 or 字段 = 值 

(例):select * from 表名 where id = 1 or name = admin;

 

//模糊查詢

like % 值%

(例):select * from 表名 where 字段 link %值%; //前后匹配

 

//隨機查詢一條數據

select * from 表名 order by rand() LIMIT 1

 

//增

 

insert into 表名 (字段1,字段2) values (值1,值2);

(例):insert into 表名 ('id','name') values ('','admin');

 

//刪

 

delete from 表名 where 字段=值;

(例):delete from 表名 where id=1;

 

delete from 表名 where id in (1,2,3);//批量刪除

 

truncate TABLE   表名;//清空表里的所有數據

 

//改

 

update 表名 set 字段=值,字段=值 where 字段=值;

(例):update 表名 set name='1' where id=1;

 

 

//雜

 

//排序

asc  正序

desc 倒序

(例):select * from 表名 where id >1 order by id asc;  //id大於1的從小到大排序

 

 

//指定位置查詢

limit 

(例):select * from 表名 where id limit 2,4;//根據id從第2條開始查詢,查詢4條

 

 

//指定條件查詢

in

(例):select * from 表名 where id in (1,2); //查詢id為1和2的數據

 

 

//指定范圍查詢

beteeen ...and...

(例):select * from 表名 where id between 3 and 5; //從id三到五之間的數據,包括三五

 

//查詢某個字段的平均值

(例):select avg(字段名) from 表名;

 

//某個字段求和

(例):select sum(字段名) from 表名;

 

//查詢某個字段值最大的數據

(例):select max(字段名) from 表名;

 

//查詢某個字段值最小的數據

(例):select min(字段名) from 表名;

 

//查詢表里數據的總條數

(例):select count(字段名) from 表名;

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM