日期:2014-05-17  浏览次数:20426 次

3表查询,同一字段区分。
SQL code
create table Goods(
id int,
name varchar(30),
price int,
sum int,
sid int,
creator varchar(20),
createtime varchar(20),
tid int
)
insert into Goods values('3','小碗熊','24','3','1','小明','2012年','3')
drop table Supplier
drop table MyTypes
drop table Goods
create table Supplier(
sid int,
name varchar(20),
linkman varchar(20),
tel varchar(20)
)
insert into Supplier values('3','小明服饰','小张','13798542123')
create table MyTypes(
tid int,
name varchar(20)
)
insert into MyTypes values('3','饼干')
select top 2 c.id,c.name,c.price,c.sum,c.creator,c.createtime,Supplier.linkman,Supplier.tel from Supplier,(select id,Goods.name,price,sum,sid,creator,createtime,MyTypes.name from Goods,MyTypes where MyTypes.tid=Goods.tid)as c where c.sid=Supplier.sid 


查询出3张表的所有字段,现在问题是3张表都有name字段,但是名称不同,也就是说查询出来会出现3个name。怎么在查的过程中区别开来。

------解决方案--------------------
起一个别名,下面的


select top 2 c.id,c.NAME AS name1,Supplier.NAME AS NAME2,c.price,c.sum,c.creator,c.createtime,Supplier.linkman,Supplier.tel 
from Supplier,
(select id,Goods.name,price,sum,sid,creator,createtime,MyTypes.name from Goods,MyTypes where MyTypes.tid=Goods.tid)as c 
where c.sid=Supplier.sid 

------解决方案--------------------
探讨
SQL code

create table Goods(
id int,
name varchar(30),
price int,
sum int,
sid int,
creator varchar(20),
createtime varchar(20),
tid int
)
insert into Goods values('3','小碗熊','24','3','1',……

------解决方案--------------------
在select 后面的name列 加as 别名

SQL code
select a.name as name1,b.name as name2 ...