简体   繁体   English

PHP / MYSQL-仅在表中显示左连接项,但在列中连接右连接值

[英]PHP / MYSQL - display only left join items in table but concatenate right join values in a column

Now I should explain briefly what I want to do. 现在,我应该简要地解释我想做什么。

I have a table of "containers" (transmittals) and another table of "items" (documents) within each container. 我在每个容器中都有一个“容器”表(传递)和另一个“项目”表(文档)。

I want to output a table listing all the containers with a column at the end of each row with a comma-space list of the items in the container. 我想输出一个列出所有容器的表,在每行的末尾有一列,其中包含容器中各项的逗号空间列表。 The purpose is to implement a simplistic search of the items in each container (using the datatables plugin). 目的是实现每个容器中项目的简单搜索(使用datatables插件)。

I could of course query my containers table and during the "PHP while loop" I could secondary query the matching container id in the items table. 我当然可以查询我的容器表,并且在“ PHP while循环”期间,我可以在项目表中辅助查询匹配的容器ID。 Then I concatenate the items for each row, but I want to avoid multiple SQL calls. 然后,我将每一行的项目串联起来,但是我想避免多次SQL调用。

So I created a right join (I write a simplified version below)... 所以我创建了一个右连接(我在下面写了一个简化版)...

SELECT transmittal.*, transmittal_documents.*
FROM transmittal RIGHT JOIN
     transmittal_documents
     ON transmittal_documents.transmittal_id = transmittal.transmittal_id

What is the best way to code my PHP while loop to only gather the unique "container" values in the table whilst concatenating the "items" using something like PHP join. 编写PHP while循环以仅收集表中唯一的“容器”值,同时使用诸如PHP join之类的“项”进行连接的最佳方法是什么?

Or, perhaps I can do this in SQL before processing in PHP? 或者,也许我可以在使用PHP进行处理之前先在SQL中执行此操作?

Thanks in advance for your assistance. 提前感谢你的帮助。

The best way is to do this in SQL using group_concat : 最好的方法是使用group_concat在SQL中执行此group_concat

SELECT t.*, group_concat(td.item) as items
FROM transmittal t LEFT JOIN
     transmittal_documents td
     ON td.transmittal_id = t.transmittal_id
GROUP BY td.transmittal_id;

I think you mean left join rather than right join . 我认为您的意思是left join而不是right join The left join will keep everything in the transmittal table along with all matches in the other table. left join会将所有内容保留在transmittal表中,并将所有匹配项保留在另一表中。

Thanks to the answer above from Gordon Linoff, I managed to get this to work for me... 多亏了戈登·利诺夫(Gordon Linoff)的上述回答,我才设法使它为我服务。

SELECT t. 选择t。 , td. ,td。 , group_concat(td.item) as items FROM transmittal t LEFT JOIN transmittal_documents td ON td.transmittal_id = t.transmittal_id GROUP BY t.transmittal_id ,group_concat(td.item)作为项目,从传输时间t左联接传输对象文档td开启td.transmittal_id = t.transmittal_id GROUP BY t.transmittal_id

I'm not sure why I needed the additional GROUP BY but it worked. 我不确定为什么我需要额外的GROUP BY,但是还是可以的。 Without it I only received one row of data. 没有它,我只会收到一行数据。

I actually had to JOIN in my additional table with the document information but that would not be an issue if the "td.item" value was a string (it was an INT in the tables I had here which caused a BLOB to be created which didn't contain the right information). 实际上,我必须在附加表中加入文档信息,但是如果“ td.item”值为字符串(这是我在此处的表中的INT导致创建BLOB的原因,那么这不是问题)没有包含正确的信息)。

It appears there may be a limit to the GROUP_CONCAT field without using additional parameter settings so I might be better handling this with Arrays in PHP. 似乎在不使用其他参数设置的情况下,GROUP_CONCAT字段可能存在限制,因此我可以使用PHP中的数组更好地处理此问题。

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

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