2014-08-28 68 views
0

我想要使用單個查詢來自所有表的數據。我怎麼可以從該表中獲取數據有效如何從多個表中有效地獲取數據?

this is my database tables

下面是我的預期輸出:

我已經試過這樣的次給錯誤。

select b_id, 
b_datetime, 
invoice_id, 
plan_id, 
exp_datetime, 
plan_name, 
plan_amount, 
months, 
s_name, 
s_url, 
name 
amount 
from tblplanboughts pb,tblplans p , tblshops s , tblusers u 
where pb.uid=u.uid && 
pb.plan_id=p.plan_id && u.uid=s.uid; 
+1

您現在如何創建輸出?你有什麼嘗試?什麼部分不起作用? Google sql加入。 – 2014-08-28 11:39:06

+0

@a_horse_with_no_name,在MySQL中我們可以使用['AND'和'&&'](http://dev.mysql.com/doc/refman/5.0/en/logical-operators.html#operator_and).. :) – 2014-08-28 12:00:25

+0

@a_horse_with_no_name,我沒有深入的知識,但是我嘗試了它的工作! :D – 2014-08-28 13:59:35

回答

1

簡單Inner join將解決你的問題:

select pb.b_datetime, pb.invoice_id, p.plan_name, p.months, pb.amount, s.s_name, s.s_url, u.name, pb.exp_datetime 
from tblplanboughts pb, tblpricingplans p, tblshops s, tblusers u 
where pn.b_id=s.b_id and pb.plan_id=p.plan_id and pb.uid=u.uid 

或者用心靈與加入關鍵字

select pb.b_datetime, pb.invoice_id, p.plan_name, p.months, pb.amount, s.s_name, s.s_url, u.name, pb.exp_datetime 
from tblplanboughts pb inner join tblpricingplans p on pb.plan_id=p.plan_id 
         inner join tblshops s on pb.b_id=s.b_id 
         inner join tblusers u on pb.uid=u.uid 
+1

由於CROSS JOIN可能是個例外,我認爲我們應該勸阻隱式連接的使用。 – Strawberry 2014-08-28 11:44:30

+0

草莓謝謝你的建議..我編輯了我的問題,無論我嘗試了什麼,還提到了我卡住的原因。 – Bhavesh 2014-08-28 11:50:26

+0

@Strawberry:即使對於交叉連接,我認爲使用明確的「CROSS JOIN」更好,因爲它證明了你確實打算這樣做。 – 2014-08-28 11:53:58

0

只需使用別名FROM子句中加入再申請加入對關係領域表格:

select pb.*, p.* , s.* , u.* 

from tblplanboughts pb, tblpricingplans p, tblshops s, tblusers u 

where pn.b_id=s.b_id and pb.plan_id=p.plan_id and pb.uid=u.uid 

您還可以使用inner join...on聲明代替使用(=),其中子句

相關問題