2013-11-20 199 views
0

我有這個表:Tables link創建一個SQL查詢

,我需要創建查詢,返回Payment_ID,門票ID或BILL ID(這取決於類型),其中業務員ID是「02」

我嘗試這樣做,但它不工作:

select p.payment_id, p.id_ticket, p.id_bill 
from Payment as p, Ticket as t, Bill as b 
where p.id_ticket=t.id_ticket 
and p.id_bill=b.id_bill 
and and t.id_salesman in ('02') 
and b.id_salesman in ('02') 

我真的很感激任何幫助。提前致謝。

+0

'和和'?你確定? – Sebas

+0

@Sebas是的,我的意思是,我猜。那應該是怎麼回事?謝謝。 –

回答

0
select p.payment_id, p.id_ticket, p.id_bill 
from Payment as p, Ticket as t, Bill as b 
where p.id_ticket=t.id_ticket 
and p.id_bill=b.id_bill 
and t.id_salesman in ('02') 
and b.id_salesman in ('02') 

你還需要加入ticketbill類似:

ticket.id = bill.id

+0

我必須?即使這兩個表格不相關? –

+0

@SergioVera您的查詢寫入的方式是。 – Woot4Moo

+0

不,我將付款與票據和賬單相關的ID,但不是相互賬單和票證:http://s2.postimg.org/tsr11vq61/Diagram1.png –