如何从多个行中选择并连接列值?

时间:2013-06-24 11:26:08

标签: php mysql

我有下表:

CREATE TABLE IF NOT EXISTS `article` (
  `article_id` int(11) NOT NULL,
  `language_id` int(11) NOT NULL,
  `category_id` int(11) NOT NULL,
  `article_title` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `article_body` varchar(8192) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `meta_description` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `meta_keyword` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `article_main_image` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `promote` bit(1) NOT NULL DEFAULT b'0',
  `created` datetime NOT NULL,
  `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted` bit(1) NOT NULL DEFAULT b'0',
  `published` bit(1) NOT NULL DEFAULT b'1',
  `user_id` int(11) NOT NULL,
  PRIMARY KEY (`article_id`,`language_id`),
  KEY `article_title` (`article_title`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

lagnuage_id:

英语= 1

阿拉伯语= 2

俄语= 3

我想select * where language_id = 1 limit by 10,但如果他们有相同的article_id,请将语言列加入一个。例如:

(1, 1, 0, 'qwq', 'da sdsad as da ds', 'asd sa d', 'as dsadasd as d sa', '', b'0', '2013-06-20 11:01:32', '2013-06-20 08:01:32', b'0', b'1', 0),
(1, 2, 0, '????????', '?? ??? ??? ??? ???', '?? ??? ??? ? ???? ', '??? ??? ?? ???? ?? ???? ', '', b'0', '2013-06-20 11:01:32', '2013-06-20 08:01:32', b'0', b'1', 0),
(1, 3, 0, 'rrrrrrrrrrr', 'rrrr', 'rrrrrrrrrrrrrrrrr', 'rrrrrrrrrrrrrrrrrrrrrrrrrr', '', b'0', '2013-06-20 11:01:32', '2013-06-20 08:01:32', b'0', b'1', 0),

我想选择这样的行:

(1, 1-2-3, 0, 'qwq', 'da sdsad as da ds', 'asd sa d', 'as dsadasd as d sa', '', b'0', '2013-06-20 11:01:32', '2013-06-20 08:01:32', b'0', b'1', 0),

注意语言id是如何连接的;我希望能够知道本文可用的语言。

1 个答案:

答案 0 :(得分:0)

使用GROUP BYGROUP_CONCAT我能够解决我的问题:

SELECT  `article_id` , GROUP_CONCAT(  `language_id` ) , ..,..,..,..
FROM  `article` 
GROUP BY  `article_id` 
LIMIT 0 , 30