有where和没有where的区别
下面两个SQL语句的区别在哪里?
select B.FItemNum,A.Famount from B left join A on A. FitemNum=B. FitemNum And A.FdataType =3 And A.Ftype = 1 And A.FdataClass = 0;
select B.FItemNum,A.Famount from B left join A on A. FitemNum=B. FitemNum where A.FdataType =3 And A.Ftype = 1 And A.FdataClass = 0;
下面两个SQL语句的区别在哪里?
select B.FItemNum,A.Famount from B left join A on A. FitemNum=B. FitemNum And A.FdataType =3 And A.Ftype = 1 And A.FdataClass = 0;
select B.FItemNum,A.Famount from B left join A on A. FitemNum=B. FitemNum where A.FdataType =3 And A.Ftype = 1 And A.FdataClass = 0;
解决方案 »
- ============ 获取XML元素节点值 ===============
- 空密码无法连接数据库
- SQL2000中文问题
- mysql5.1的数据如何导入到sqlserver2008?
- msde安装的问题,在线急等
- 请高手指点一下ADO,OLEDB,ODBC,DAO,RDO的区别!初学,我被搞晕了!
- 不知有没有这样的SQL语句写法,如果有,具体该怎么写呢,多谢了
- 如何用SqlServer导入*.dat数据文件?(急!急!)
- 紧急求助 sql server 2005 同步复制(非局域网内)
- [急]将sqlserver的一个表的数据随机取出50条导入access中,这个语句应该怎么写??
- 【求助】更新完SQL2005SP3后IIS出现问题
- clementine 12 怎么连接sql server 2005
第二个是过滤掉不符合条件的记录
(必须返回左边表的记录)了,条件不为真的就全部过滤掉。
declare @ta table([id] int,[size] int)
insert @ta
select 1,10 union all
select 2,20 union all
select 3,30--> 测试数据:@tb
declare @tb table([size] int,[name] varchar(3))
insert @tb
select 10,'AAA' union all
select 20,'BBB' union all
select 20,'CCC'select * from @ta a left join @tb b on a.[size]=b.[size] and a.[id]=1
-------------------结果---------
/*
id size size name
----------- ----------- ----------- ----
1 10 10 AAA
2 20 NULL NULL
3 30 NULL NULL
*/select * from @ta a left join @tb b on a.[size]=b.[size] where a.[id]=1
------------------------结果------------
/*
id size size name
----------- ----------- ----------- ----
1 10 10 AAA
*/
----------------------------------------
第二个经过左连接后 再用where中A表限定的条件 过滤
就是说 A表的条件起了过滤的作用~
输入某一年,即查询出该年内1-12个月中的商品数目,没有商品的月显示为0,显示效果如下所示(可建立临时表进行左外连接)。
T_Product表内有三个字段,其中时间显示年月日
产品ID ,产品名称 ,添加时间输入2007年,则显示如下
月份 商品个数
1 ###
2 ###
3 ###
4 ###
5 ###
6 ###
7 ###
8 ###
9 ###
10 ###
11 ###
12 ###
if object_id('[T_Product]') is not null drop table [T_Product]
go
create table [T_Product]([产品ID] int,[产品名称] varchar(3),[添加时间] datetime)
insert [T_Product]
select 1,'abc','2009-1-2' union all
select 13,'abc','2008-12-2' union all
select 14,'abc','2009-12-2'
---查询---
declare @year int;
set @year=2009
select 月份=1,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=1),0)
union all
select 月份=2,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=2),0)
union all
select 月份=3,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=3),0)
union all
select 月份=4,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=4),0)
union all
select 月份=5,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=5),0)
union all
select 月份=6,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=6),0)
union all
select 月份=7,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=7),0)
union all
select 月份=8,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=8),0)
union all
select 月份=9,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=9),0)
union all
select 月份=10,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=10),0)
union all
select 月份=11,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=11),0)
union all
select 月份=12,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=12),0)---结果---
月份 商品个数
----------- -----------
1 1
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 1(所影响的行数为 12 行)
if object_id('[T_Product]') is not null drop table [T_Product]
go
create table [T_Product]([产品ID] int,[产品名称] varchar(3),[添加时间] datetime)
insert [T_Product]
select 1,'abc','2009-1-2' union all
select 13,'abc','2008-12-2' union all
select 14,'abc','2009-12-2' --假如输入 2009
select id,[产品名称],[添加时间] from
(select (number+1) as id from master.dbo.spt_values where type = 'p' AND number < 12) a
left join
(select * from [T_Product] where year([添加时间])='2009') b on a.id=datepart(mm,[添加时间])
/*
id 产品名称 添加时间
----------- ---- -----------------------
1 abc 2009-01-02 00:00:00.000
2 NULL NULL
3 NULL NULL
4 NULL NULL
5 NULL NULL
6 NULL NULL
7 NULL NULL
8 NULL NULL
9 NULL NULL
10 NULL NULL
11 NULL NULL
12 abc 2009-12-02 00:00:00.000(12 行受影响)
*/
我当时写的是这样,这样的话如果当月没有产品,就没有统计了
SELECT DATEPART(MONTH,createdate),COUNT(DATEPART(MONTH,createdate))
FROM dbo.t_product
WHERE DATEPART(yyyy,createdate) = '2007'
GROUP BY DATEPART(MONTH,createdate)
if object_id('[T_Product]') is not null drop table [T_Product]
go
create table [T_Product]([产品ID] int,[产品名称] varchar(3),[添加时间] datetime)
insert [T_Product]
select 1,'abc','2009-1-2' union all
select 13,'abc','2008-12-2' union all
select 14,'abc','2009-12-2'
------------------查询开始---------------------
--假如输入 2009
select a.[月份],isnull(b.[商品个数],0) as[商品个数] from
(select (number+1) as [月份] from master.dbo.spt_values where type = 'p' AND number < 12) a
left join
(select month([添加时间]) as [月份],count(1) as [商品个数] from [T_Product] where year([添加时间])='2009' group by month([添加时间])) b
on a.[月份]=b.[月份]
----------------------------结果--------------------------------
/*
月份 商品个数
----------- -----------
1 1
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 1(12 行受影响)*/
select B.FItemNum,A.Famount from B left join A on A. FitemNum=B. FitemNum where A.FdataType =3 And A.Ftype = 1 And A.FdataClass = 0; 第二个语句比第一个区别是查询出的记录必须存在B表中