简体   繁体   English

如何将一个表中另一个表具有多个ID的两个表联接在一起?

[英]How to join two tables with multiple IDs from one table used in other table?

I need to join two tables tableA and tableB , but the problem is that in tableB I have two IDs from tableA and I need to join them so I get both values. 我需要tableA两个表tableAtableB ,但是问题是在tableB我有来自tableA两个ID ,因此我需要将它们tableA起来,以便同时获得两个值。 I would like to achieve it using Codeigniter's active record class. 我想使用Codeigniter的活动记录类来实现它。 Here is a small demo: 这是一个小演示:

tableA:
╔══════════╦════════════╗
║ video_id ║ video_name ║
╠══════════╬════════════╣
║        1 ║ short      ║
║        2 ║ long       ║
║        3 ║ black      ║
║        4 ║ white      ║
╚══════════╩════════════╝

tableB
╔═════════╦════════╦════════╗
║ pair_id ║ pair_a ║ pair_b ║
╠═════════╬════════╬════════╣
║       1 ║      1 ║      2 ║
║       2 ║      1 ║      4 ║
║       3 ║      4 ║      3 ║
║       4 ║      3 ║      2 ║
╚═════════╩════════╩════════╝

this doesn't work: 这不起作用:

$this->db->select('*')
         ->from("tableA")
         ->join('tableB AS A', 'tableA.video_id = A.pair_a')
         ->select("tableA.video_name as VIDEO_A")

         ->join('tableB AS B', 'tableA.video_id = B.pair_b')
         ->select("tableA.video_name as VIDEO_B")

         ->get()
         ->result();

The final result needs to be something like this: 最终结果需要是这样的:

stdClass Object
(
    [0] => stdClass Object
        (
            [VIDEO_A] => short
            [VIDEO_B] => long
        )

    [1] => stdClass Object
        (
            [VIDEO_A] => short
            [VIDEO_B] => white
        )

    [2] => stdClass Object
        (
            [VIDEO_A] => white
            [VIDEO_B] => black
        )

    [3] => stdClass Object
        (
            [VIDEO_A] => black
            [VIDEO_B] => long
        )

)

You mix up tableA and tableB in your query. 您在查询中混合了tableA和tableB。 For each item in B you want something, so start joining there. 对于B中的每个项目,您都想要一些东西,所以从那里开始加入。 Then, join renamed table, and select the items from the renamed tables. 然后,加入重命名的表,并从重命名的表中选择项目。

->from("tableB")
->join('tableA AS pairA', 'pairA.video_id = tableB.pair_a')
->join('tableA AS pairB', 'pairB.video_id = tableB.pair_b')
->select("pairA.video_name as VIDEO_A, pairB.video_name as VIDEO_B")

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

相关问题 PHP mysql如何连接两个表来从一个表链接名称和从其他表发布 - PHP mysql how to join two tables to link name from one table and post from other table mysql-将一个表与另外两个表联接 - mysql - join one table with two other tables 使用Codeigniter将表中的一行与MySQL中其他两个表的多行联接 - Join one row from a table with multiple rows of two other tables in MySQL using Codeigniter 如何加入多个表并分组并从一张表中获得前 2 名 - How to join multiple tables and group by and get top 2 from one table mysql join查询从两个表中选择行,一个表有多个与第一个表匹配的行 - mysql join query for select rows from two tables that one table is having multiple rows matching to the first table PHP + MySQL-一个表保存对多个表中ID的引用 - PHP + MySQL - one table holding reference to IDs from multiple tables 如何连接两个表,然后显示从另一个表收集的表的每个数据的总和值 - How to join two tables and then display the sum value of each data of a table gathering from the other table MySQL连接两个表,一个表具有多个匹配行 - mysql join two tables that one table is having multiple matching rows 将两个表连接在一起,一个表具有多行匹配 - Join two tables with one table has multiple rows matching 在多个ID上联接表 - Join table on multiple ids
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM