读书人

请问两个SQL语句~

发布时间: 2012-02-01 16:58:19 作者: rapoo

请教两个SQL语句~~~
两个多对多关系的表:employer与employee 还有一个中间连接表:employment

mysql> desc employer;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| employerid | int(11) | | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+

mysql> desc employment;
+--------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+----------+------+-----+---------+----------------+
| employmentid | int(11) | | PRI | NULL | auto_increment |
| startDate | datetime | YES | | NULL | |
| endDate | datetime | YES | | NULL | |
| employerid | int(11) | | MUL | 0 | |
| employeeid | int(11) | | MUL | 0 | |
+--------------+----------+------+-----+---------+----------------+

mysql> desc employee;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| employeeid | int(11) | | PRI | NULL | auto_increment |


| name | varchar(255) | YES | | NULL | |
| taxfileNumber | varchar(255) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+

为何执行以下两句查询之后的查询结果却大相径庭?望大家详细指点下!谢谢!
select * from employer er left join employment et on er.employerid = et.employerid right join employee ee on et.employeeid = ee.employeeid;

select * from (select * from employer er left join employment et on er.employerid = et.employerid) ss right join employee ee on ss.employeeid = ee.employeeid;


[解决办法]
我测试了一下数据,两个结果是一样的

declare @t1 table(id1 int identity(1,1),mm1 varchar(3))
insert into @t1
select '22 '
union all
select '44 '
union all
select 'aa '

declare @t2 table(id2 int identity(1,1),mm2 varchar(3))
insert into @t2
select 'bb '
union all
select 'cc '
union all
select 'dd '


declare @t3 table(id3 int identity(1,1),mm3 varchar(3))
insert into @t3
select 'MM '
union all
select 'ac '
union all
select 'bb '
union all
select 'cc '
union all
select 'dd '


select * from @t1 A left join @t2 B on A.id1=B.id2 right join @t3 C on A.ID1=C.ID3

select * from (select * from @t1 A left join @t2 B on A.id1=B.id2) D right join @t3 C on D.ID1=C.ID3

[解决办法]
这个我昨天也测试了十几组数据,都一样所以没有回帖(LZ之前发过同样一帖的好像)
[解决办法]
在.NET好象看到帖子。

建你的以及果出看看

读书人网 >SQL Server

热点推荐