简体   繁体   English

将两个表连接在一起,一个表具有多行匹配

[英]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

Sample fiddle 样品小提琴

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.

相关问题 MySQL连接两个表,一个表具有多个匹配行 - mysql join two tables that one table is having multiple matching rows mysql join查询从两个表中选择行,一个表有多个与第一个表匹配的行 - mysql join query for select rows from two tables that one table is having multiple rows matching to the first table 使用Codeigniter将表中的一行与MySQL中其他两个表的多行联接 - Join one row from a table with multiple rows of two other tables in MySQL using Codeigniter 如何插入两个表; 一个将插入 1 行,另一个将插入多行,两个表的一列具有相同的值 - how to insert into two tables; one will insert 1 row and the other one will insert multiple rows, the two tables has one column with the same value mysql-将一个表与另外两个表联接 - mysql - join one table with two other tables MYSQL连接两个表,第二个表具有匹配的值 - MYSQL join two tables, with matching value from second table 如何将一个表中另一个表具有多个ID的两个表联接在一起? - How to join two tables with multiple IDs from one table used in other table? 将具有多行的表联接到一行 - Join table with multiple rows to one row 在MySQL中,需要将两个表连接起来,其中一个表对第二个表有多个引用 - In MySQL, need to join two tables with one table having multiple references to the second 在MySQL 5 TABLES中使用INNER JOIN删除多个表中的多行 - Delete multiple rows in multiple table using INNER JOIN in mySQL 5 TABLES
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM