[英]Join two tables with one table has multiple rows matching
Trying to figure out if it is possible to create a query where you join tables, table one is smaller than table two, table two has multiple references matching table one entries, the query would output a joining where table one length is preserved but you just add more columns. 尝试找出是否有可能在连接表的地方创建查询,表一小于表二,表二具有与表一条目匹配的多个引用,查询将输出一个联接,其中表一的长度被保留,但您只是添加更多列。 Not sure if that makes sense so here is a example of what I am after 不知道这是否有意义,所以这是我所追求的一个例子
Table One Table two
+-----------------------------+ +-----------------------------+
| id | english | definition | | id | word_id | sentence |
+-----------------------------+ +-----------------------------+
|1 | A1 | blah | |1 | 1 | blahblah1 |
|2 | B4 | blah2 | |2 | 1 | blahblah2 |
+-----------------------------+ |3 | 1 | blahblah3 |
|4 | 2 | blahblah4 |
|5 | 2 | blahblah5 |
+-----------------------------+
********* Query should return something like ***************** *********查询应返回类似******************的内容
+----------------------------------------------------------------+
| id | english | definition | sentence | sentence2 | sentence3 |
+----------------------------------------------------------------+
|1 | A1 | blah | blahblah1| blahblah2| blahblah3 |
|2 | B4 | blah2 | blahblah4| blahblah5| |
+----------------------------------------------------------------+
My Current query looks like this and results in: 我的当前查询如下所示,并导致:
$query = "SELECT * FROM T1 INNER JOIN T2 ON T1.id = T2.word_id";
Resulting in: 导致:
+----------------------------------------+
| id | english | definition | sentence |
+----------------------------------------+
|1 | A1 | blah | blahblah1|
|1 | A1 | blah | blahblah2|
|1 | A1 | blah | blahblah3|
|2 | B4 | blah2 | blahblah4|
|2 | B4 | blah2 | blahblah5|
+----------------------------------------+
I am working with PHP and MySql. 我正在使用PHP和MySql。
UPDATE!! UPDATE!
Staying with my original query and manipulating the results with PHP getting good performance too. 保持我原来的查询并使用PHP处理结果也可以获得良好的性能。 Let me know if you need me to post my code. 让我知道您是否需要我发布我的代码。
You might be looking for GROUP_CONCAT() 您可能正在寻找GROUP_CONCAT()
SELECT T1.id, T1.english,T1.definition,
GROUP_CONCAT(T2.sentence ORDER BY T2.ID SEPARATOR '|')
FROM Table1 T1 INNER JOIN Table2 T2 ON T1.id = T2.word_id
Group by word_id
I will take the risk of a down vote and say the answer is NO. 我冒着投反对票的危险,说答案是否定的。 Simply because there's unknown number of columns (say, if you add one more sentence to the top word you could be adding one more column to the result set) and each column is not well defined ( Why blahblah4 should be in column sentence instead of sentence2 ?) 仅仅因为列数不明(例如,如果您在最上面的单词中再增加一个句子,您可能会在结果集中再增加一列),并且每个列的定义都不明确(为什么blahblah4应该在列句子中而不是句子2中) ?)
IMO, SQL is used to tell what you want to get, but in this case, SQL is unable to tell exactly what you want. IMO,SQL用来告诉您想要获得什么,但是在这种情况下,SQL无法准确告诉您想要得到什么。
Even if this can be done ( I would love to learn ), I believe the complexity offsets any benefit and handling this in PHP is a better option. 即使可以做到这一点(我想学习),我相信复杂性会抵消任何好处,因此在PHP中进行处理是一个更好的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.