简体   繁体   English

通过带有变量且存在的mysql select语句进行迭代

[英]iterate through mysql select statement with variable and while exists

I have a bit of code where I am setting a variable based on a select statement, and then want to change the parameter so that the all the rows affected in the select statement create one long string in the variable. 我有一些代码,我在其中基于select语句设置变量,然后想要更改参数,以便select语句中受影响的所有行在变量中创建一个长字符串。

In other words, something like this: 换句话说,是这样的:

set @crstype1 = ''; 设置@ crstype1 =''; select t1.col2 from t1 inner join t2 on t1.col1 = t2.col1 where t2.id = 1; 从t1.col1 = t2.col1的t1内部联接t2中选择t1.col2,其中t2.id = 1;

//select stmt provides several rows affected. // select stmt提供受影响的几行。 now I want to make all the rows in one long comma separated string 现在我想将所有的行放在一个长逗号分隔的字符串中

while exists [select statement] do set @crstype1 = @crstype1 + ',' end while; while存在[select语句]设置@ crstype1 = @ crstype1 +','结束while;

I know my syntax is all messed up. 我知道我的语法都搞砸了。 Any ideas on how to do this? 有关如何执行此操作的任何想法? I'm thinking maybe a for...each would be better? 我想也许是...每个都会更好?

Thanks. 谢谢。

Use the GROUP_CONCAT() function in MYSQL for this. 为此,请在MYSQL中使用GROUP_CONCAT()函数。

Something like: 就像是:

SELECT GROUP_CONCAT(col1)
FROM tbl1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM