从phpmyadmin中的多个表中检索数据

时间:2014-11-12 00:29:36

标签: sql phpmyadmin

我有3张桌子,所有桌子都与PK和FK联系在一起。表格为acc_details,acc_info,acc_bill

TABLE : acc_info

ID     |    acc_no         |  rate
______________________________________
1      |         00001     |     0
2      |         00002     |     21
3      |         00003     |     21
4      |         00004     |     21 

TABLE : acc_details

id_dls |    acc_type       |  address         |  **ID**
________________________________________________________
1      |        store      |     pekan        |  1
2      |     water plant   |     kuantan      |  2
3      |       store       |     kuantan      |  2
4      |      pump house   |     kuantan      |  4


TABLE : acc_bill

id_bill |        acc_no    |  charge_1        |  charge_2
________________________________________________________
1       |       00001      |     20.00        |  12.00
2       |       00002      |     15.00        |  16.00
3       |       00004      |    200.00        |  22.00

PK ---> ID,acc_no FROM acc_info FK ---> ID FROM acc_details,acc_no FROM acc_bill

我希望结果是这样的

address     |    acc_no       |    rate   |  charge_1  | charge_2
_________________________________________________________________________________
pekan       |    00001        |     0     |   20.00    |   12.00
kuantan     |    00002        |    21     |   15.00    |   16.00
kuantan     |    00004        |    21     |  200.00    |   22.00

现在,我试过这个

SELECT address , acc_no , rate , charge_1 , charge_2
FROM acc_info , acc_details , acc_bill
WHERE acc_info.id = acc_details AND acc_info.acc_no = acc_bill.acc_no

但错误#1052 - 字段列表中的列'acc_no'不明确 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

SELECT address ,
       acc_info.acc_no , 
       rate , 
       charge_1 , 
       charge_2
FROM acc_info 
JOIN acc_details ON acc_info.id = acc_details.id
JOIN acc_bill ON acc_info.acc_no = acc_bill.acc_no

这意味着acc_no存在于两个连接的表中,因此您需要指定您想要的acc_no