工作不正常

lrl1mhuk  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(442)

我想把两张table连起来。用表b中相应的值显示表a。

ID   |   NAMES
        1    |   jan
        2    |   san
        3    |   tani
        4    |   mike

表B

ID   |   VALUE
       1    |   groen
       1    |   geel
       1    |   oranja
       2    |   groen

查询

SELECT * FROM TableA a LEFT JOIN TableB b ON a.id = b.id

输出

1 - jan - groen
      1 - jan - geel
      1 - jan - oranja
      2 - san - groen
      3 - tani
      4 - mike

所需输出

1 - jan - groen, geel, oranja
      2 - san - groen
      3 - tani
      4 - mike

如果我正确理解连接,我已经按照每个可能的顺序(select tablea.id,tablea.names tableb.id…等等)将查询更改为每个可能的事物(left join,left inner join,left outer join,right join,right inner join,right outer join,full join,join),我要找的连接类型是inner left join—用tableb的相应值显示tablea。
我正在使用MySQL5.6

xzv2uavs

xzv2uavs1#

尝试此查询

SELEct a.id,a.name, 
 GROUP_CONCAT(b.name)name 
 FROM a left JOIN b ON (a.id = b.id)
 GROUP BY a.id
svdrlsy4

svdrlsy42#

考虑以下几点:

<?php

/*
DROP TABLE IF EXISTS tablea;

CREATE TABLE tablea
(ID SERIAL PRIMARY KEY
,NAME VARCHAR(12) NOT NULL UNIQUE
);

INSERT INTO tablea VALUES
(1,'jan'),
(2,'san'),
(3,'tani'),
(4,'mike');

DROP TABLE IF EXISTS tableb;

CREATE TABLE tableb
(ID   INT NOT NULL
,VALUE VARCHAR(12) NOT NULL
,PRIMARY KEY(id,value)
);

INSERT INTO tableb VALUES
(1,'groen'),
(1,'geel'),
(1,'oranja'),
(2,'groen');

SELECT a.id
     , a.name
     , b.value
  FROM tablea a
  LEFT
  JOIN tableb b
    ON b.id = a.id
 ORDER
    BY id
     , value;
+----+------+--------+
| ID | NAME | value  |
+----+------+--------+
|  1 | jan  | geel   |
|  1 | jan  | groen  |
|  1 | jan  | oranja |
|  2 | san  | groen  |
|  3 | tani | NULL   |
|  4 | mike | NULL   |
+----+------+--------+

* /

require('path/to/connection/stateme.nts');

$query = "
SELECT a.id
     , a.name
     , b.value
  FROM tablea a
  LEFT
  JOIN tableb b
    ON b.id = a.id
 ORDER
    BY id
     , value;
";

$result = mysqli_query($db,$query) or die(mysqli_error());

$old_array = array();

while($row = mysqli_fetch_assoc($result)){

$old_array[] = $row;

}

$new_array = array();

foreach ($old_array as $row) {
   $new_array[$row['id']]['name'] = $row['name'];
   $new_array[$row['id']]['value'][] = $row['value'];
}

$new_array = array_values($new_array); // reindex

print_r($new_array);

?>

输出:

Array
(
    [0] => Array
        (
            [name] => jan
            [value] => Array
                (
                    [0] => geel
                    [1] => groen
                    [2] => oranja
                )

        )

    [1] => Array
        (
            [name] => san
            [value] => Array
                (
                    [0] => groen
                )

        )

    [2] => Array
        (
            [name] => tani
            [value] => Array
                (
                    [0] =>
                )

        )

    [3] => Array
        (
            [name] => mike
            [value] => Array
                (
                    [0] =>
                )

        )

)

相关问题