- 1、本文档共31页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
常用SQL语句的语法及实例讲解
数据操纵语言--单表查询 单表查询 单表查询是指只涉及一个表的查询。 1.查询指定列: select o.rowid,o.ordertype from [order] o; 2.查询全部的列:select * from [order]; 3. 查询非重复的记录:select distinct o.rowid,o.ordertype from [order] o; 4.用where字句筛选查询记录: select * from [order] where ordertype=1; 5. 使用分组、集合函数:select t.ordertype,COUNT(OrderType) from [Order] t group by OrderType having COUNT(OrderType) 1 下表是一些常用的查询条件 (注意:查询条件当中,null前边不能跟=,!=): 数据操纵语言--Where 字句 用where字句建立筛选标准,即是θ连接,是一种旧的连接。 Where字句可以用在select、update、delete、insert……select、create view等查询或者子查询当中。前面提到的一些常用的查询条件,在where子句中都适用。 例子演示: select * from [order] where ordertype=1 select * from [order] where ordertype between 1 and 2 select * from [order] where ordertype in (1,2,0); select * from [order] where ordercode like %ord%; select * from [order] where ordertype is not null; select * from [order] where ordertype=1 and ordercode like %ord%; insert into customer2(customername) select customername from customer where rowid1; …… 数据操纵语言---连接查询 若一个查询,同时涉及两个或者两个以上表,称之为连接查询。 连接查询主要分为三类:1.笛卡尔积连接;2.内连接;3.外连接; 笛卡尔积连接 笛卡尔积连接不带WHERE 子句,它返回被连接的两个表所有数据行的笛卡尔积,返回到结果集合中的数 据行数等于第一个表中符合查询条件的数据行数乘以第二个表中符合查询条件的数据行数。(这种连接不具实际意义,日常极少使用) 内连接 内连接查询操作列出与连接条件匹配的数据行。 外连接 外连接,返回到查询结果集合的不仅包含符合连接条件的行,而且还包括左表(left join)、右表(right join),两个表(full join)中的所有数据行。 数据操纵语言--连接查询 笛卡尔积连接 Select * from [order],orderdetail; 内连接 Select * from [order] o inner join orderdetail od on o.rowid=od.orderid; select re1.regioncode 上级区域编码,re1.regionname 上级区域名称,re2.regioncode 区域编码,re2.regionname 区域名称 from region re1 inner join region re2 on re1.rowid=re2.parentregionid …… 数据操纵语言--连接查询 左连接:返回包括左表中的所有记录和右表中连接字段相等的记录,左边表没有匹 配时,右表为空。 select cu.RowID,cu.CustomerName,cp.RowID,cp.CustomerID,cp.SKU_ID from Customer cu left join customerproduct cp on cu.rowid=cp.customerid 右连接:返回包括右表中的所有记录和左表中连接字段相等的记录,右边表没有匹配时,显示为空。 select cu.RowID,cu.CustomerName,cp.RowID,cp.CustomerID,cp.SKU_ID from customerproduct cp right join Customer cu on cu.rowid=cp.customerid 全连接:返回包括左、右表中的所有记录,左边或者右边表没有匹
文档评论(0)