MySql INNER JOIN,ERROR#1066

时间:2014-12-12 09:28:11

标签: php mysql

我有3个数据库。

grameenphone_bill »:tbl_admin - > CP_ID

banglalink_bill »:tbl_billing_response-> CPID

robi_bill »:tbl_billing_response-> CPID

所有三个db的表都有两个常见的字段,即ChargedAmount和cpid。 登录通过 grameenphone_bill »:tbl_admin完成。

现在,我想通过使用cpid

来计算收入,即ChargedAmount

我写了一个查询但收到错误 #1066 - 不是唯一的表/别名:'tbl_admin'

这是我写的查询。

SELECT grameenphone_bill.tbl_admin.cp_id,
       grameenphone_bill.tbl_admin.cp_name,
       banglalink_bill.tbl_billing_response.cpid,
       robi_bill.tbl_billing_response.cpid,
       grameenphone_bill.tbl_billing_response.ChargedAmount,
       banglalink_bill.tbl_billing_response.ChargedAmount,
       robi_bill.tbl_billing_response.ChargedAmount
FROM grameenphone_bill.tbl_admin
INNER JOIN grameenphone_bill.tbl_admin ON grameenphone_bill.tbl_admin.cp_id=banglalink_bill.tbl_billing_response.cpid
INNER JOIN banglalink_bill.tbl_billing_response ON banglalink_bill.tbl_billing_response.cpid=robi_bill.tbl_billing_response.cpid

请告诉我,我哪里出错了。 谢谢!

3 个答案:

答案 0 :(得分:1)

join不正确 - 您不需要加入tbl_admin,它已经在您的from条款中了:

SELECT     grameenphone_bill.tbl_admin.cp_id,
           grameenphone_bill.tbl_admin.cp_name,
           banglalink_bill.tbl_billing_response.cpid,
           robi_bill.tbl_billing_response.cpid,
           grameenphone_bill.tbl_billing_response.ChargedAmount,
           banglalink_bill.tbl_billing_response.ChargedAmount,
           robi_bill.tbl_billing_response.ChargedAmount
FROM       grameenphone_bill.tbl_admin
INNER JOIN banglalink_bill.tbl_billing_response ON 
           grameenphone_bill.tbl_admin.cp_id = 
           banglalink_bill.tbl_billing_response.cpid
INNER JOIN robi_bill.tbl_billing_response ON 
           banglalink_bill.tbl_billing_response.cpid = 
           robi_bill.tbl_billing_response.cpid

答案 1 :(得分:0)

您正在使用grameenphone_bill.tbl_admin本身加入grameenphone_bill.tbl_admin。我认为应该是robi_bill.tbl_billing_response。

SELECT grameenphone_bill.tbl_admin.cp_id,
       grameenphone_bill.tbl_admin.cp_name,
       banglalink_bill.tbl_billing_response.cpid,
       robi_bill.tbl_billing_response.cpid,
       grameenphone_bill.tbl_billing_response.ChargedAmount,
       banglalink_bill.tbl_billing_response.ChargedAmount,
       robi_bill.tbl_billing_response.ChargedAmount
FROM grameenphone_bill.tbl_admin
INNER JOIN banglalink_bill.tbl_billing_response ON grameenphone_bill.tbl_admin.cp_id=banglalink_bill.tbl_billing_response.cpid
INNER JOIN robi_bill.tbl_billing_response ON banglalink_bill.tbl_billing_response.cpid=robi_bill.tbl_billing_response.cpid

希望这有帮助。

答案 2 :(得分:0)

当你在一个选择中选择一个表两次时,你应该给它至少一个别名:

SELECT gta.cp_id,
       gta.cp_name,
       ba.cpid,
       rb.cpid,
       gta.ChargedAmount,
       ba.ChargedAmount,
       rb.ChargedAmount
FROM grameenphone_bill.tbl_admin gta
INNER JOIN banglalink_bill.tbl_billing_response ba ON gta.cp_id=ba.cpid
INNER JOIN robi_bill.tbl_billing_response rb ON ba.cpid=rb.cpid

我希望你的问题能够解决。