我有4个表,它们相互之间的联系方式如下: TB1 <-TB2 <-TB3 <-TB4 这意味着TB4拥有TB3中属于单个行的元素,TB3拥有TB2中属于单个行的元素,最后TB2拥有TB1中属于单个行的数据。 我做了这个图,试图使它更清楚
(编辑:数据库假设是表中的TB)
我尝试通过使用以下子查询来实现此目的:
SELECT TB1.id AS TB1_ID,
(SELECT TB2.id AS TB2_ID,
(SELECT TB3.id AS TB3_ID,
(SELECT TB4.id AS TB4_ID
FROM `TB4` AS TB4 WHERE TB4.TB3_id = TB3.id) AS C
FROM `TB3` AS TB3 WHERE TB3.TB2_id = TB2.id) AS B
FROM `TB2` AS TB2 WHERE TB2.TB1_id = TB1.id) AS A
FROM `TB1` AS TB1
但是我的逻辑肯定有缺陷:或者我缺少有关查询相关数据的信息:因为这返回null,即使我知道表包含进行这种交叉组合所需的必要信息。
所需结果是数组中的一组嵌套数组:每个表一个嵌套数组。这样我们最终得到一个像这样的结构:
{*, A{*, B{*, C{*} } } }
因此TB1中的每一行都包含TB2中元素的多维数组作为变量,而TB2中的每一行形式都包含TB3中元素的多维数组作为元素,依此类推...
我还尝试过将所有信息作为单独的查询,然后将它们加入JS中,但是事实证明这很繁琐:因此,如果有人知道如何以正确的方式做到这一点,我将非常感谢-非常感谢前进
PS。我通过使用XAMPP在我的本地环境中尝试:这会造成问题吗?
答案 0 :(得分:2)
我认为您想要的是一系列JOIN
:
SELECT TB1.id AS TB1_ID, TB2.id AS TB2_ID, TB3.id AS TB3_ID, TB4.id AS TB4_ID
FROM TB1
JOIN TB2 ON TB2.TB1_ID = TB1.ID
JOIN TB3 ON TB3.TB2_ID = TB2.ID
JOIN TB4 ON TB4.TB3_ID = TB3.ID
然后,您可以使用以下类似的方法在PHP中构建所需的结构:
$sql = "SELECT TB1.id AS TB1_ID, TB2.id AS TB2_ID, TB3.id AS TB3_ID, TB4.id AS TB4_ID
FROM TB1
JOIN TB2 ON TB2.TB1_ID = TB1.ID
JOIN TB3 ON TB3.TB2_ID = TB2.ID
JOIN TB4 ON TB4.TB3_ID = TB3.ID";
$result = $conn->query($sql) or die($conn->error);
$output = array();
while ($row = $result->fetch_assoc()) {
$tb1_id = $row['TB1_ID'];
$tb2_id = $row['TB2_ID'];
$tb3_id = $row['TB3_ID'];
$tb4_id = $row['TB4_ID'];
if (isset($output[$tb1_id][$tb2_id][$tb3_id])) {
$output[$tb1_id][$tb2_id][$tb3_id][$tb4_id] = array();
}
elseif (isset($output[$tb1_id][$tb2_id])) {
$output[$tb1_id][$tb2_id][$tb3_id] = array($tb4_id => array());
}
elseif (isset($output[$tb1_id])) {
$output[$tb1_id][$tb2_id] = array($tb3_id => array($tb4_id => array()));
}
else {
$output[$tb1_id] = array($tb2_id => array($tb3_id => array($tb4_id => array())));
}
}
答案 1 :(得分:1)
这里是尝试使用JSON函数和其他应该在最新5.7版本中运行的东西。
但是不要问,因为我从这个实验中发现,在MySql 5.7中处理嵌套json是真正的PITA。
样本数据:
drop table if exists Table1;
drop table if exists Table2;
drop table if exists Table3;
drop table if exists Table4;
create table Table1 (id int primary key, col1 varchar(30));
create table Table2 (id int primary key, tbl1_id int, col1 varchar(30));
create table Table3 (id int primary key, tbl2_id int, col1 varchar(30));
create table Table4 (id int primary key, tbl3_id int, col1 varchar(30));
insert into Table1 (id, col1) values
(101, 'A1'),(102, 'A2'),(103, 'A3'),(104, 'A4');
insert into Table2 (id, tbl1_id, col1) values
(201, 101, 'B1'), (202, 102, 'B2'),(203, 103, 'B3');
insert into Table3 (id, tbl2_id, col1) values
(301, 201, 'C1'),(302, 202, 'C2');
insert into Table4 (id, tbl3_id, col1) values
(401, 301, 'D1'), (402, 301, 'D2');
查询:
SELECT t1.id AS t1id,
GROUP_CONCAT(REPLACE(JSON_OBJECT(t1.id, JSON_ARRAY(t1.col1)),']}',', '),
IFNULL(
(
SELECT
GROUP_CONCAT(
REPLACE(JSON_OBJECT(t2.id, JSON_ARRAY(t2.col1)),']}',', '),
IFNULL(
(
SELECT
GROUP_CONCAT(
REPLACE(JSON_OBJECT(t3.id, JSON_ARRAY(t3.col1)),']}',', '),
IFNULL(
(
SELECT
CONCAT('[',
IFNULL(GROUP_CONCAT(JSON_OBJECT(t4.id, JSON_ARRAY(t4.col1))),''),
']') D
FROM Table4 t4
WHERE t4.tbl3_id = t3.id
GROUP BY t4.tbl3_id
), '[]'), ']}') C
FROM Table3 t3
WHERE t3.tbl2_id = t2.id
GROUP BY t3.tbl2_id
), '[]'), ']}') B
FROM Table2 t2
WHERE t2.tbl1_id = t1.id
GROUP BY t2.tbl1_id
), '[]'), ']}') A
FROM Table1 t1
GROUP BY t1.id;
返回:
id A
101 {"101": ["A1", {"201": ["B1", {"301": ["C1", [{"401": ["D1"]},{"402": ["D2"]}]]}]}]}
102 {"102": ["A2", {"202": ["B2", {"302": ["C2", []]}]}]}
103 {"103": ["A3", {"203": ["B3", []]}]}
104 {"104": ["A4", []]}
对 db <>小提琴here
的测试