2014-08-28 63 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声明代替使用(=),其中子句

相关问题