简体   繁体   English

选择列条目作为一个字符串

[英]Select column entries as one string

I have table like this: 我有这样的表:

CREATE TABLE IF NOT EXISTS `test` (
    `a` int(11) NOT NULL,
    `b` int(11) NOT NULL,
    `c` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO `test` (`a`, `b`, `c`) VALUES
(1, 2, 3),
(4, 5, 6),
(7, 8, 9),
(10, 11, 12),
(13, 14, 15);

Which statement would be get out a string like this "1,4,7,10,13"? 哪个语句会得到像这样的字符串“ 1、4、7、10、13”? I don't understand what one of contact functions i need, and how to use it right, i get anytime errors or wrong strings 我不明白我需要联系函数中的哪一个以及如何正确使用它,我会随时出现错误或错误的字符串

You could use GROUP_CONCAT() . 您可以使用GROUP_CONCAT() In this example, this would work. 在这个例子中,这将起作用。 Obviously you would probably need add a WHERE clause to it. 显然你可能需要为它添加一个WHERE子句。

SELECT GROUP_CONCAT(a)

FROM   test

Also if you wanted to do this for multiple groups you would want to use a GROUP BY clause. 此外,如果您想为多个组执行此操作,您可能希望使用GROUP BY子句。

试试GROUP_CONCAT

SELECT GROUP_CONCAT(a) FROM test;

What you're looking for is the function: GROUP_CONCAT 您正在寻找的功能是: GROUP_CONCAT

SELECT GROUP_CONCAT(a) AS result_list
FROM test

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

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