组合三个表使用另一个表进行交叉引用

时间:2013-05-14 05:51:08

标签: php mysql sql arrays

背景

作为我的mySQL n00b,我可以提出匹配三个表的最佳查询,运行两个表之间的比较,输出一个变量,然后使用该变量从交叉引用表中选择我的最终输出。之后,我会运行另一个查询来从我的第三个表中输出...

现在我知道有一种方法可以只用一个语句来选择所需的所有行,但是对于我的生活,我无法将它拼凑在一起。有人可以帮我构建我需要的查询吗?

我必须使用3个表中的信息在php中输出结果集,并使用单独的表作为两个表的ID之间的实际链接。谢谢!!

name: table_one
-----------------------------------------------------------
id | user_id | o_id | num | likes | dislikes | .... | ... |
-----------------------------------------------------------
 1 | 765     | 1    | 100 |android| cats                  |
 2 | 765     | 2    | 100 | birds | mySQL queries         |
 3 | 765     | 3    | 100 | php   | iPhones               |
 4 | 765     | 2    |  2  |oranges| bananas               |
-----------------------------------------------------------

name: table_two                            
------------------------------------------------------------|
id |first_name| location | num_times | diploma | why        |
------------------------------------------------------------|
1 | ABC     | here     | 0         | none    | because      |
2 | BCD     | there    | 5         | BS      | no reason    |
3 | Sally   | nowhere  | 194384    | DR      | no reason    |
4 | Jack    | overthere| 3         | none    | failure      |
5 | Bob     | Mars     | 0         | random  | in training  |
-------------------------------------------------------------

name: table_agency         |
---------------------------|
id | name | address        | 
---------------------------|
1  | A    | 123x           | 
2  | B    | 234y           | 
3  | C    | 456z           | 
----------------------------
name: table_link 
-----------------------------
          rel_a | rel_b           
---------------------------------
           1   |    1            |
           1   |    4            |
           1   |    5            |
           2   |    1            |
           2   |    4            |
           2   |    5            |
           3   |    2            |
           3   |    3            |
           4   |    3            |
--------------------------------- 

输出/ PHP

$results = $class->runQuery($query);    //basically a fetchAll
foreach ($results as $result) { 
echo id_table_one ($result['id']);              
echo $result['name'];
echo $result['num'];
echo $result['likes'];
echo other_rows...basically table_one.*
echo all_first_names&num_times that correspond in the table link;
}

    //ACTUAL Printout(echo doesn't have the ,'s):
    //here should be the output:

-------------------------------------------------
1 | A     | 100  | android  | ABC-0/Jack-3/Bob-0|
2 | B     | 100  | birds    | ABC-0/Jack-3/Bob-0|
3 | C     | 100  | php      | BCD-5/Sally-194384|
4 | B     | 2    | oranges  | Sally-194384      |
----------------------------------------------  

附注

1)至少有一个交叉引用项目条目与名字相关联,最多7个条目(不必查询,但供参考)

2)table_agency

中只有3个项目条目

3)结果集必须符合:user_id=".$variable

4)我的原始查询,或多或少......但是由于我试图拉动的附加信息之间没有共性,我被迫创建了一个能够打平,....了解我想要实现的目标:

$query = "select
          a.*,
          b.name,
          b.id   as agency_id
      from table_one a,  table_agency b
      where a.agency_id = b.id
      and a.user_id = ".$variable;

现在使用新添加的first_names实际上与$result['name']

对应

5)我可以使用嵌套的foreach(来实际输出first_name的嵌套数组的最终结果集???

6)请对查询(回复)进行评论,以便我可以从您的所有努力中学习!我不是只想回答问题,而是学习步骤和方法!

7)提前感谢你的帮助......这绝对是一个头脑风暴。感谢!!!

1 个答案:

答案 0 :(得分:3)

您可以使用此查询获取输出

SELECT
    ta.id,
    ta.name,
    to.num,
    to.likes,
    GROUP_CONCAT(tt.first_name SEPARATOR '-') AS `names`
FROM table_agency AS ta
LEFT JOIN table_one AS `to` ON to.o_id = ta.id
LEFT JOIN table_link AS tl ON tl.rel_a = to.id
LEFT JOIN (SELECT id , first_name FROM table_two) AS tt ON tt.id = tl.rel_b
WHERE to.user_id = 765
GROUP BY to.id

您可以使用变量替换php代码中的user_id。对于ABC-0/Jack-3/Bob-0,您可以简单地替换此GROUP_CONCAT(tt.first_name SEPARATOR '/') AS names

SQL Fiddle Demo

输出

| ID | NAME | NUM |   LIKES |        NAMES |
--------------------------------------------
|  1 |    A | 100 | android | ABC-Jack-Bob |
|  2 |    B | 100 |   birds | Bob-ABC-Jack |
|  3 |    C | 100 |     php |    BCD-Sally |
|  2 |    B |   2 | oranges |        Sally |

编辑:

这是编辑过的查询。您可以使用MySQL Concat功能

SELECT
    ta.id,
    ta.name,
    to.num,
    to.likes,
    GROUP_CONCAT(tt.first_name SEPARATOR '/') AS `names`
FROM table_agency AS ta
LEFT JOIN table_one AS `to` ON to.o_id = ta.id
LEFT JOIN table_link AS tl ON tl.rel_a = to.id
LEFT JOIN (
          SELECT 
          id , 
          CONCAT(first_name,'-',num_times) as first_name 
          FROM table_two
        ) AS tt ON tt.id = tl.rel_b
GROUP BY to.id; 

Demo

输出

| ID | NAME | NUM |   LIKES |              NAMES |
--------------------------------------------------
|  1 |    A | 100 | android | ABC-0/Jack-3/Bob-0 |
|  2 |    B | 100 |   birds | ABC-0/Jack-3/Bob-0 |
|  3 |    C | 100 |     php | BCD-5/Sally-194384 |
|  2 |    B |   2 | oranges |       Sally-194384 |