Please give me a SQL Statement...(200分)

  • 主题发起人 主题发起人 netfool
  • 开始时间 开始时间
N

netfool

Unregistered / Unconfirmed
GUEST, unregistred user!
table1 -- store product information
------------------------------------------------
FieldName DataType
------------------------------------------------
ID AutoNumber(PK)
Name Text
Price Double
...
------------------------------------------------

table2 -- store houseware information
------------------------------------------------
FieldName DataType
------------------------------------------------
ID AutoNumber(PK)
Name Text
...
------------------------------------------------

table3 -- the middle table
------------------------------------------------
FieldName DataType
------------------------------------------------
Name Text
HouseID Integer(FK is [table2].[ID]
ProdID Number(FK is [table1].[ID])
ProdCount Integer
------------------------------------------------

ok, now I want to use a sql to create a dataset that include:
1) table2.name, table1.Name, table1.Price, table3.ProdCount
2) even if table3 has not any record of (table1.id, table2.id), but they
shell be in the dateset

(* just like 'GROUP BY table2.Name, table1.Name')

I had tryed use the 'JOIN' keyword of sql, but I don't known how to??? :(

any answer please let me know, thank you first!
 
select c.ProdCount, a.Name as aName, b.Name as bName from table3 as c inner join
table1 as a on c.ProdID=a.ID inner join table2 as b on c.houseID=b.ID
 
你提的要求无法满足的,关联表中没有的记录,你就没法将house和product同时
查询到一张表上!
以下两种可以分别得到所有house和所有product的情况:
select c.ProdCount, a.Name as aName, b.Name as bName from table1 as a
left outer join table3 as c on a.id=c.ProdID left outer join table2 as b
on b.id=c.houseid

select c.ProdCount, a.Name as aName, b.Name as bName from table2 as b
left outer join table3 as c on b.id=c.houseid left outer join table1 as a
on a.id=c.ProdID
 

Similar threads

I
回复
0
查看
589
import
I
I
回复
0
查看
2K
import
I
I
回复
0
查看
1K
import
I
I
回复
0
查看
3K
import
I
I
回复
0
查看
3K
import
I
后退
顶部