我有两张桌子:
Customer:
customerid - int, pri-key,auto
fname - varchar
sname -varchar
housenum - varchar
streetname -varchar
items
itemid - int,pri-key,auto
type - varchar
collectiondate - date
releasedate - date
customerid - int
我需要一个sql查询来获取给定客户的所有项目,包括一些客户详细信息,特别是:fname + sname + type + collectiondate + releaseate + housenum + streetname。
例如,如果joe bloggs有5个项目,那么将返回5行,列将如上所述
提前感谢。
答案 0 :(得分:1)
select customer.*,
items.*
from
customer,
items
where
customer.customerid = items.customerid
order by
customer.customerID
答案 1 :(得分:1)
select a.customerid, fname, sname, type, collectiondate, releasedate, housenum, streetname
from customer as a
inner join items as b on b.customerid = a.customerid
更新#1
select a.customerid, fname, sname, type, collectiondate, releasedate, housenum, streetname
from customer as a
inner join items as b on b.customerid = a.customerid
where a.customerid = 4 //or whatever number or variable
答案 2 :(得分:1)
select c.fname, c.sname, i.type, i.collectiondate, i.releasedate, c.housenum, c.streetname
from customers c inner join items i
on c.customerid = i.customerid
通常不建议使用“*”,因为如果将新列添加到表中,则返回的数据集将更改,并且因为它使您依赖于表中列的顺序,是否指定列您自己的名字将所有控制权交给您,使应用程序更灵活。
此外,最好只使用JOIN而不是列出表名。