|
楼主 |
发表于 2006-9-8 22:01
|
显示全部楼层
原帖由 joe_233 于 2006-5-10 21:33 发表
小考一下吧
now,the data in table temp
year comm_type commission
--------------------------------------------------------
2001 CASH ...
今天看SQL的书,突然想起史前师兄给我的考题,于是做着试试看.
费了很大劲,终于出结果,虽然方法可能很笨.
在MYSQL下:
- create table tempTable(year varchar(50),comm_type varchar(50),commission varchar(50));
- insert into tempTable values("2001","CASH","123,456");
- insert into tempTable values("2001","CHECK","234,567");
- insert into tempTable values("2001","AUTO","345,678");
- insert into tempTable values("2002","CASH","223,456");
- insert into tempTable values("2002","CHECK","334,567");
- insert into tempTable values("2002","AUTO","445,678");
- insert into tempTable values("2003","CASH","323,456");
- insert into tempTable values("2003","CHECK","334,567");
- insert into tempTable values("2003","AUTO","345,678");
- insert into tempTable values("2004","CASH","423,456");
- insert into tempTable values("2004","CHECK","434,567");
- insert into tempTable values("2004","AUTO","445,678");
复制代码
然后:
- select tmp1.year,tmp1.cash,tmp2.auto,tmp3.check from
- (select year,commission as "cash" from temptable where comm_type="CASH" ) as tmp1
- join
- (select year,commission as "AUTO" from temptable where comm_type="AUTO") as tmp2
- join
- (select year,commission as "Check" from temptable where comm_type="CHECK") as tmp3
- where tmp1.year=tmp2.year and tmp2.year=tmp3.year;
- +------+---------+---------+---------+
- | year | cash | auto | check |
- +------+---------+---------+---------+
- | 2001 | 123,456 | 345,678 | 234,567 |
- | 2002 | 223,456 | 445,678 | 334,567 |
- | 2003 | 323,456 | 345,678 | 334,567 |
- | 2004 | 423,456 | 445,678 | 434,567 |
- +------+---------+---------+---------+
复制代码
得出的结果只是列序不同. |
|