问题
I'm using the query suggested in this post https://stackoverflow.com/a/51816820/6822845 to list my table contents. This is working really fine, I get a list of every section with its subsections comma seperated in another column so I can explode it and convert it to an array. My problem is, that my sections table(mentioned under my other post linked above) has another column named "sorder" that holds the displaying order. I don't know why, but I'm not able to output it with the column selects as its every time 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
Every time I run it sorder displays as "0". But it's not 0. The weird thing is, that I can read the "iorder" column which is in my sub_section table. But the "sorder"-column which is in the main-table "sections" isn't accessible / every time 0. I'm using Mysql:
回答1:
I've created an SQLFiddle which I hope represents your data. In that example, ORDER BY sorder works fine. I've also included an ORDER BY in the GROUP_CONCAT so that subsection titles can be ordered to. So, with the original query:
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
The output is
t1 t2
Section One SubOne,SubTwo
Section Three SubThree
Section Two (null)
But with the new query:
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
The output is:
sorder t1 t2
1 Section Three SubThree
2 Section One SubTwo,SubOne
3 Section Two (null)
Note reordering of Section Three and Section One and also SubTwo and SubOne based on the values of sorder and iorder.
来源:https://stackoverflow.com/questions/51817626/php-sql-left-join-statement-lost-one-column