简体   繁体   English

获得三个联接表的列

[英]get colums of three joined tables

I was able to join 3 tables and count the data set, bet getting the columns on specific table it only see the 3rd table column.. 我能够加入3个表并计算数据集,并赌弄到特定表上的列只能看到第3个表列。

 SELECT * 
 FROM `InvoiceItemTable` a 
 JOIN `InvoiceTable` b 
   ON (b.id = a.invoice) 
 JOIN `products` c 
   ON (a.product = c.id)  
 WHERE b.status='1' AND c.store = '2'
 //$invoices = $inginger->fetch(PDO::FETCH_ASSOC))
 echo $invoices['a.price'];

This price return error : Undefined index: a.price in... there is "price" in the first table(InvoiceItemTable). 此价格返回错误:未定义的索引:a.price在...的第一个表(InvoiceItemTable)中有“ price”。 echo $invoices['invoice']; echo $ invoices ['invoice']; There is invoice in first table(InvoiceItemTable) and it returns it works I dont want to use $invoices['price'] because there is 'price' colum in the third table too and that is what it returns, I want to get the price in the first table. 第一个表(InvoiceItemTable)中有发票,它返回它可以工作,我不想使用$ invoices ['price'],因为在第三张表中也有'price'列,这就是它返回的内容,我想获取价格在第一张表中。 $invoices['price.InvoiceItemTable'] returns undefined index $ invoices ['price.InvoiceItemTable']返回未定义的索引

php wont recognize $invoices['a.price']; php不会识别$invoices['a.price']; you have to use $invoices['price']; 您必须使用$invoices['price'];

if you have same fieldname in multiple tables you have to create an alias 如果在多个表中具有相同的字段名,则必须创建一个别名

 SELECT a.price as a_price, b.price as b_price, c.price as c_price

and then you can use $invoices['a_price'] 然后您可以使用$invoices['a_price']

In general, it's much better to explicitly define your columns. 通常,最好显式定义列。 In doing so you can add a specific name to the the column you're interested in (or conversely change the name of the one you're not to remove ambiguity) 这样,您可以在感兴趣的列中添加一个特定的名称(或者相反地,更改您不想删除的含混不清名称的名称)

For examples sake, I'm just showing the columns you've described but it would look something like. 例如,我只是显示您描述的列,但看起来像。

SELECT a.price as price, c.price as product_price
FROM `InvoiceItemTable` a 
JOIN `InvoiceTable` b ON (b.id = a.invoice) 
JOIN `products` c ON (a.product = c.id)
WHERE b.status='1' AND c.store = '2';

Doing naming such as this is more work, but insulates your code from changes and more explicitly shows the reader what is being returned. 进行这样的命名是更多的工作,但是可以使您的代码免受更改的影响,并且可以更清晰地向读者显示返回的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM