我需要帮助来构建结果查询:
TBL。主
id | Title
205 | Main_A
206 | Main_B
207 | Main_C
208 | Main_D
209 | Main_E
210 | Main_F
211 | Main_G
212 | Main_H
TBL。子:
id | parent__main_id | Title
401 | 205 | Sub_A
402 | 205 | Sub_B
403 | 207 | Sub_C
404 | 209 | Sub_D
405 | 209 | Sub_E
406 | 210 | Sub_F
407 | 211 | Sub_G
408 | 212 | Sub_H
TBL。 Cat和Pro由Tbl支持。子:
TBL。猫:
id | parent__sub_id | Title
808 | 401 | Cat_A
809 | 401 | Cat_B
810 | 401 | Cat_C
811 | 401 | Cat_D
812 | 403 | Cat_E
813 | 404 | Cat_F
TBL。临
id | parent__sub_id | Title
920 | 402 | Pro_A
921 | 402 | Pro_B
922 | 405 | Pro_C
923 | 406 | Pro_D
924 | 406 | Pro_E
925 | 406 | Pro_F
我想得到以下输出:
205 | Main_A | 401 | Sub_A | 808 | Cat_A
205 | Main_A | 401 | Sub_A | 809 | Cat_B
205 | Main_A | 401 | Sub_A | 810 | Cat_C
205 | Main_A | 401 | Sub_A | 811 | Cat_D
205 | Main_A | 402 | Sub_B | 920 | Pro_A
205 | Main_A | 402 | Sub_B | 921 | Pro_B
206 | Main_B | NULL | NULL | NULL | NULL
207 | Main_C | 403 | Sub_C | 812 | Cat_E
208 | Main_D | NULL | NULL | NULL | NULL
209 | Main_E | 404 | Sub_D | 813 | Cat_F
209 | Main_E | 405 | Sub_E | 922 | Pro_C
209 | Main_F | 406 | Sub_F | 923 | Pro_D
209 | Main_F | 406 | Sub_F | 924 | Pro_E
210 | Main_F | 406 | Sub_F | 925 | Pro_F
211 | Main_G | 407 | Sub_G | NULL | NULL
212 | Main_H | 408 | Sub_H | NULL | NULL
有人能帮助我吗?感谢...
刚多
答案 0 :(得分:1)
这应该有效:
SELECT m.id AS mid,
m.Title AS mtitle,
s.id AS sid,
s.Title AS stitle,
c.id AS cid,
c.Title AS ctitle
FROM Main m
JOIN Sub s
ON m.id == s.parent__main_id
JOIN Cat c
ON s.id == c.parent__sub_id
JOIN Pro p
ON s.id == p.parent__sub_id;
答案 1 :(得分:0)
看起来您需要在同一结果列中附加case var n when n >= 100:
和Cat
个表格的标题列,在这种情况下,您可以使用Pro
和{{1}的两个查询他们,例如:
LEFT JOIN
这将为您提供所需的结果,但不会按主要ID的顺序。如果您希望结果排序,那么您可以将此查询包装到另一个查询中,并通过例如:
来应用顺序UNION
答案 2 :(得分:0)
CREATE TABLE `Tbl_Main` (
`id` int(11) NOT NULL,
`Title` char(20) COLLATE utf8_unicode_ci NOT NULL
) ;
insert into Tbl_Main (id,Title) values ( 205 , "Main_A" ), ( 206 , "Main_B" ), ( 207 , "Main_C" ), ( 208 , "Main_D" ), ( 209 , "Main_E" ), ( 210 , "Main_F" ), ( 211 , "Main_G" ), ( 212 , "Main_H" );
CREATE TABLE `Tbl_Sub` (
`id` int(11) NOT NULL,
`parent__main_id` int(11) NOT NULL,
`Title` char(20) COLLATE utf8_unicode_ci NOT NULL
);
insert into Tbl_Sub (id,parent__main_id,Title) values ( 401 , 205 , 'Sub_A' ), ( 402 , 205 , 'Sub_B' ), ( 403 , 207 , 'Sub_C' ), ( 404 , 209 , 'Sub_D' ), ( 405 , 209 , 'Sub_E' ), ( 406 , 210 , 'Sub_F' ), ( 407 , 211 , 'Sub_G' ), ( 408 , 212 , 'Sub_H' );
CREATE TABLE `Tbl_Cat` (
`id` int(11) NOT NULL,
`parent__sub_id` int(11) NOT NULL,
`Title` char(20) COLLATE utf8_unicode_ci NOT NULL
);
insert into Tbl_Cat (id,parent__sub_id,Title) values ( 808 , 401 , "Cat_A" ), ( 809 , 401 , "Cat_B" ), ( 810 , 401 , "Cat_C" ), ( 811 , 401 , "Cat_D" ), ( 812 , 403 , "Cat_E" ), ( 813 , 404 , "Cat_F" );
CREATE TABLE `Tbl_Pro` (
`id` int(11) NOT NULL,
`parent__sub_id` int(11) NOT NULL,
`Title` char(20) COLLATE utf8_unicode_ci NOT NULL
);
insert into Tbl_Pro (id,parent__sub_id,Title) values ( 920 , 402 , "Pro_A" ), ( 921 , 402 , "Pro_B" ), ( 922 , 405 , "Pro_C" ), ( 923 , 406 , "Pro_D" ), ( 924 , 406 , "Pro_E" ), ( 925 , 406 , "Pro_F" );
select Tbl_Main.* , sub1.sub_id, sub1.sub_title, sub1.last_id, sub1.last_title from Tbl_Main
left join
(
(
select s.id 'sub_id' , s.Title 'sub_title' , s.parent__main_id , l.id 'last_id' , l.Title 'last_title'
from Tbl_Sub s
join Tbl_Cat l
on s.id = l.parent__sub_id
) UNION (
select s.id 'sub_id' , s.Title 'sub_title' , s.parent__main_id , l.id 'last_id' , l.Title 'last_title'
from Tbl_Sub s
join Tbl_Pro l
on s.id = l.parent__sub_id
)
) sub1
on Tbl_Main.id = sub1.parent__main_id
order by Tbl_Main.id , sub1.sub_id , sub1.last_id;
'205','Main_A','401','Sub_A','808','Cat_A'
'205','Main_A','401','Sub_A','809','Cat_B'
'205','Main_A','401','Sub_A','810','Cat_C'
'205','Main_A','401','Sub_A','811','Cat_D'
'205','Main_A','402','Sub_B','920','Pro_A'
'205','Main_A','402','Sub_B','921','Pro_B'
'206','Main_B',NULL,NULL,NULL,NULL
'207','Main_C','403','Sub_C','812','Cat_E'
'208','Main_D',NULL,NULL,NULL,NULL
'209','Main_E','404','Sub_D','813','Cat_F'
'209','Main_E','405','Sub_E','922','Pro_C'
'210','Main_F','406','Sub_F','923','Pro_D'
'210','Main_F','406','Sub_F','924','Pro_E'
'210','Main_F','406','Sub_F','925','Pro_F'
'211','Main_G',NULL,NULL,NULL,NULL
'212','Main_H',NULL,NULL,NULL,NULL
我立刻发现了两件事
所以解决方案就像是
Tbl_Main join ( Tbl_Cat Union Tbl_Pro )
但是缺少将Tbl_Main的引用连接到Tbl_Cat或Tbl_Pro所需的东西,那就是Tbl_Sub。
以及解决方案是什么:
Tbl_Main join ( ( Tbl_Sub join Tbl_Cat ) Union ( Tbl_Sub join Tbl_Pro ) )
干杯!