开发者

Mqsql Query question

开发者 https://www.devze.com 2023-02-01 14:05 出处:网络
sorry i am a sql noob. I have 2 tables: Customer: customerid - int, pri-key,auto fname - varchar sname -varchar

sorry i am a sql noob.

I have 2 tables:

Customer:
customerid - int, pri-key,auto
fname - varchar
sname -varchar
housenum 开发者_如何学Python- varchar
streetname -varchar

items
itemid - int,pri-key,auto
type - varchar
collectiondate - date
releasedate - date
customerid - int

I need a sql query which will get me all items for a given customer including some customer details, specifically: fname + sname + type + collectiondate + releasedate + housenum + streetname.

for example, if joe bloggs has 5 items then 5 rows will be returned and the columns will be as described above

thanks in advance.


select a.customerid, fname, sname, type, collectiondate, releasedate, housenum, streetname 
from customer as a
inner join items as b on b.customerid = a.customerid

Update #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


select customer.*,
       items.*
   from
       customer,
       items
   where
       customer.customerid = items.customerid
   order by
       customer.customerID


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

It is generally not recommended to use "*" because then your returned dataset will change if a new column is ever added to the table, and because it makes you dependent on the order of columns in the table, whether specifying the column names by yourself leaves all the control in your hands and makes the application more flexible to changes.

Besides, it is better to specifically use JOIN than just list the table names.

0

精彩评论

暂无评论...
验证码 换一张
取 消