php sql left join语句丢失一列

jgzswidk  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(484)

我正在使用这篇文章中建议的查询https://stackoverflow.com/a/51816820/6822845 列出我的表格内容。这是非常好的工作,我得到了一个列表,每个部分的子部分逗号分隔在另一列中,这样我就可以分解它,并将其转换为一个数组。我的问题是,我的sections表(在上面链接的另一篇文章中提到)有另一个名为“sorder”的列,它保存了显示顺序。我不知道为什么,但我无法将它与列selects一起输出为每次0。

SELECT sections.sorder as sorder ,section_titel as t1,
       GROUP_CONCAT(sub_section_titel) as t2 
FROM sections
    LEFT JOIN sub_sections ON section_id = sId
GROUP BY t1
ORDER by sorder

每次我运行它排序显示为“0”。但不是0。奇怪的是,我可以读取subïsection表中的“iorder”列。但是主表“sections”中的“sorder”列不可访问/每次0。我正在使用mysql:

nuypyhwy

nuypyhwy1#

我创建了一个sqlfiddle,希望它能代表您的数据。在那个例子中, ORDER BY sorder 很好用。我还包括一个 ORDER BYGROUP_CONCAT 这样子节标题可以被命令。因此,对于原始查询:

SELECT section_titel as t1, GROUP_CONCAT(sub_section_titel) as t2 
FROM sections LEFT JOIN sub_sections ON section_id = sId
GROUP BY t1

输出为

t1              t2
Section One     SubOne,SubTwo
Section Three   SubThree
Section Two     (null)

但对于新的查询:

SELECT sorder, section_titel as t1, GROUP_CONCAT(sub_section_titel ORDER BY iorder) as t2 
FROM sections LEFT JOIN sub_sections ON section_id = sId
GROUP BY t1
ORDER BY sorder

输出为:

sorder  t1              t2
1       Section Three   SubThree
2       Section One     SubTwo,SubOne
3       Section Two     (null)

注:重新排序 Section Three 以及 Section One 还有 SubTwo 以及 SubOne 基于 sorder 以及 iorder .

相关问题