1、创建数据库:createdatabase2、创建数据表:createtable3、基本的写法:增(insert)、删(delete)、改(update)、查(select)Select*from表名where条件或连接条件---查询表中数据Insert表名(‘’,’’,’’,’’)values(‘’,’’,’’,’’)---------------在行间添加数据Delete表名where条件-------------删除某表中的某一行数据Update表名set列名=’’-------------修改某表中的某一列数据4、基于基本写法而进行的拓展补充:1)模糊查询(Like/%{0}%)例:只输入商品名的部分文字就可以查看商品信息(用like做模糊查询)。
select*from Goods where g_Name like'%果壳%'2)连接条件(内连接查询)例:按商品类别查看“图书”类商品信息。
商品名、状态、原价、折扣、折后价。
select g_Name,g_zhuangtai,g_Price,g_zhekou,g_Price*g_zhekou from Goods,s hangpinTypes where t_Name='图书'and shangpinTypes.t_ID=Goods.t_ID 3)求和函数(Sum)例:统计每一张订单的总金额,显示订单编号、总金额。
select o_ID,sum(o_Sum)from Ordersgroup by o_ID4)分组(group by)例:按天统计每一种商品的销售额。
select o_Data,t_ID,sum(xiangqing_Price*xiangqing_Number)from Orders,Ordersxiangqing,Goodswhere Orders.o_ID=Ordersxiangqing.o_IDand Ordersxiangqing.g_ID=Goods.g_IDand o_Data='2014-3-17'groupby t_ID,o_Data5)年月例:按月统计商城的销售额。
select year(o_Data),month(o_Data),sum(o_Sum)from Orderswhere o_Data>='2014-1-1'and o_Data<='2014-12-31'groupby year(o_Data),month(o_Data)6)统计后进行的条件(having)例:当客户购物总金额大于一定额度,修改客户的级别。
update huiyuanxinxibiao set kehuTypewhere kehu_IDin(select kehu_ID from Ordersgroupby kehu_IDhaving sum(o_Sum)>=100)7)排列(Order by/asc/desc)例:查询每一类商品的总金额,并按照商品总额进行降序排列select t_ID,sum(o_Sum)from Orders,Ordersxiangqing,Goodswhere Orders.o_ID=Ordersxiangqing.o_IDand Ordersxiangqing.g_ID=Goods.g_IDgroupby t_IDorderby sum(o_Sum)desc8)获取当时的时间(Getdate())例:需要了解所有年龄在~岁之间的会员的名称、籍贯和年龄。
select kehuName,kehuAddressfrom huiyuanxinxibiaowhere(year(getdate())-year(kehuBirth))<20or(year(getdate())-year(kehuBirth))>259)与Like连用的另一个连接符(_),表示一个字符例:需要了解姓“黄”且名字中只有两个汉字的会员的会员名、真实姓名、电话和电子邮箱。
select kehuName,kehuTrueName,kehuPhone,kehuE_mailfrom huiyuanxinxibiaowhere kehuTrueName like'黄_'10)消除重复(distinct)及统计(count)例:需要了解在WebShop网站进行了购物并下了订单的会员编号,如果一个会员下了多个订单,只需要显示一次会员编号。
select count(distinct Orders.o_ID)from Orders,Ordersxiangqing,huiyu anxinxibiaowhere Ordersxiangqing.o_ID=Orders.o_IDand huiyuanxinxibiao.kehu_ID=Orders.o_ID11)子查询例:需要了解和“摩托罗拉W380”为同类商品的商品号、商品名称和类别号。
select g_ID,g_Name,t_IDfrom Goodswhere t_ID=(select t_ID from Goods where g_Name='摩托罗拉W380')orderby t_ID12)使用汉字标题例:查询WebShop数据库中男“VIP会员”的详细情况,并对所有的列使用汉字标题。
select kehu_ID'客户编号',kehuName'用户名',kehuTrueName'客户真实姓名',(2014-year(kehuBirth))'客户年龄',kehuPassWord'客户密码'from huiyuanxinxibiaowhere kehuType='VIP'and kehuSex='男'13)外连接查询(right join/on)例:统计Orders表中每一会员的订单总额.select kehuName,o_ID,sum(o_Sum)from Orders right join huiyuanxinxibiaoon Orders.kehu_ID=huiyuanxinxibiao.kehu_IDgroup by kehuName,o_ID14)主键(primary key)例:createtable PayTypes(p_ID char(2)notnull primarykey,p_Typemingcheng varchar(20)notnull,p_shuoming varchar(100)null)15)DATEDIFF()函数Datepart,startdate,enddat例:用图书条形码查看本书状态为“未还”的借还记录,显示读者编号、读者名、--图书条形码、图书名称、借书时间、应还时间、超期天数、罚款金额selectJieHuanInfo.r_ID,r_Name,BookCunFangInfo.TiaoXing_ID,b_Name,JieYue_Out Date,JieYue_InDate,DATEDIFF(dd,'2014-3-1',getdate()),rt_ChaoQiFaJin fromReaderInfo,JieHuanInfo,BookInformation,ReaderType,BookCunFangInfo where ReaderType.rt_ID=ReaderInfo.rt_IDand BookCunFangInfo.TiaoXing_ID=JieHuanInfo.TiaoXing_IDand BookCunFangInfo.b_ID=BookInformation.b_IDand JieHuanInfo.r_ID=ReaderInfo.r_IDand BookCunFangInfo.TiaoXing_ID='12345671'and JieYue_status='未还' 16)DATEADD(datepart,number,date)例:用读者编号查看读者现借未还的图书信息,显示读者名、图书编号、图书名、出版社、作者、存放位置、到期时间。
(***)selectr_Name,BookInformation.b_ID,p_Name,b_Writer,CF_Position,dateadd(dd,rt _XianJieQiXian,JieYue_OutDate)'到期时间'fromBookInformation,PChuBanShe,JieHuanInfo,BookCunFangInfo,ReaderType,Rea derInfowhere BookInformation.b_ID=BookCunFangInfo.b_IDand PChuBanShe.p_ID=BookInformation.p_IDand BookCunFangInfo.TiaoXing_ID=JieHuanInfo.TiaoXing_IDand ReaderType.rt_ID=ReaderInfo.rt_IDand JieHuanInfo.r_ID=ReaderInfo.r_IDand ReaderInfo.r_ID='r0007'and JieYue_status='未还'17)涉及空值的查询例:select*from Goods where g_Image is null18)指定范围查询例:20-25岁间会员的名称、籍贯和年龄Select c_Name,c_Address,Year(GetDate())-Year(c_Birth)from Customers where Year(GetDate())-Year(c_Birth)between20and25。