假设我有下表:
new Promise((resolve, reject) => {
resolve("1");
}).then((v1) => {
console.log("v1");
return new Promise((resolve, reject) => {
setTimeout(() => {resolve(2)}, 3000)
}).then((v11) => {
console.log("v11");
return new Promise((resolve, reject) => {
setTimeout(() => {resolve(2)}, 4000)
}).then(() => {
console.log("v12")
})
})
}).then((v2) => {
console.log("v2")
});
当我使用如下查询时:
ID|Col1 |Col2
1 |Test1 |Test12
2 |Test2 |Test22
2 |Test3 |Test32
它有时会返回GROUP_CONCAT重新排列。例如:
SELECT GROUP_CONCAT(Col1) as First, GROUP_CONCAT(Col2) as Second WHERE ID=2 GROUP BY ID
虽然我希望它能回归:
ID|First |Second
2 |Test2,Test3|Test32,Test22
如您所见,它在名为' Second'的列中切换了连接值。我怎样才能防止这种情况发生?
答案 0 :(得分:1)
如果您希望按特定顺序排列值,请使用order by
:
SELECT GROUP_CONCAT(Col1 ORDER BY Col1) as First,
GROUP_CONCAT(Col2 ORDER BY col2) as Second
FROM t
WHERE ID = 2
GROUP BY ID;
通常,SQL处理无序集。如果您希望按特定顺序排列,则需要明确指定排序。
答案 1 :(得分:1)
此表缺少要在group_concat中排序的公共列(PK)。
ID | GroupID | Col1 | Col2
1 | 1 | Test1 | Test12
2 | 2 | Test2 | Test22
3 | 2 | Test3 | Test32
SELECT GROUP_CONCAT(Col1 ORDER BY ID) as First,
GROUP_CONCAT(Col2 ORDER BY ID) as Second
FROM t
WHERE GroupID = 2
GROUP BY GroupID;
这将保留col1和col2的组连续顺序。