zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

PHP之Mysql常用SQL语句示例的深入分析

mysqlPHPSQL 示例 语句 常用 深入分析
2023-06-13 09:15:00 时间
1.插入数据
insertinto表名(列名1,列名2,列名..)values(值1,2,值...);  insertintoproduct(name,price,pic_path)values("Nike",500,"uploads/3245.jpg");

2.更新数据
update表名set列名1=值1,列名2=值2[where条件]; 
updateproductsetname="LiNing",price=50whereid=2; 

3.删除数据
deletefrom表名[where条件]; 
deletefromproductwhereid=2;
4.查询所有数据 
select*from表名;select*fromproduct;
5.查询部份列
select列名1,列名2,列名Nfrom表名; selectname,pricefromproduct;
6.条件查询
#比较=,<,>,<=,>=,!= select*from表名where列名=值; select*fromproductwhereid=2; #and与  select*from表名where条件1and条件2and条件N;  select*fromproductwherename="Nike"andprice=50; #or或  select*from表名where条件1or条件2or条件N; select*fromproductwherename="Nike"orprice>50;  #not非  select*from表名wherenot条件1; select*fromproductwherenotname="Nike";  #in枚举  select*from表名where列名in(值1,2,N);  select*fromproductwhereidin(2,3,4,10);  select*fromproductwhereidnotin(2,3,4,10);  #like模糊查询  select*from表名where列名like"%值%";  select*fromproductwherenamelike"%Li%";  #between...and...范围查询  select*from表名where列名betweenand值;  select*fromorderwherecreatedbetween"2010-01-01"and"2011-01-01";
7.查询排序
select*from表名orderby列名排序方式; #排序方式:asc(升序,默认),desc(降序)  select*fromproductorderbycreateddesc;
8.限制查询结果数量
select*from表名limit开始记录数,结果数量;select*fromproductlimit5; select*fromproductlimit2,5;
9.聚合函数
#count总记录数  selectcount(列名)fromstudent;  selectcount(id)fromstudent;  #sum总共  selectsum(列名)fromstudent;  selectsum(age)fromstudent;  #avg平均值  selectavg(列名)fromstudent; selectavg(age)asavg_agefromstudent;  #max最大值  selectmax(列名)fromstudent;  selectmax(age)fromstudent;  #min最小值  selectmin(列名)fromstudent;  selectmin(age)fromstudent;
10.子查询
selectnamefromstudentwhereage<(selectavg(age)fromstudent);  select*fromproductwhereidin(selectidfromorder);
11.连接查询
selects.usernameasstu_name,t.nameaste_namefromstudents,teachertwheres.teacher_id=t.id;