有两个表,结构如下:
A表
CREATE TABLE IF NOT EXISTS `A` (
`id` int(11) NOT NULL auto_increment COMMENT 'ID',
`name` char(200) collate utf8_unicode_ci NOT NULL COMMENT '指标名称'
)
INSERT INTO `A` (`id`, `cname`) VALUES
(8, '新闻'),
(18, '娱乐'),
(10, '电影'),
(11, '小说');B表
CREATE TABLE IF NOT EXISTS `B` (
`id` int(11) NOT NULL auto_increment,
`cid` int(11) NOT NULL COMMENT '指标ID',
`name` char(200) NOT NULL COMMENT '名称'
)
INSERT INTO `B` (`id`, `cid`, `name`) VALUES
(6, 18, 'ddddddd'),
(13, 10, 'sssssss'),
(7, 18, 'ccccccc'),
(14, 18, 'eeeeeee'),
(9, 11, 'fffffff'),
(10, 10, 'wwwwww'),
(11, 8, 'eeeerrrrrr'),
(15, 11, 'hhhhhhhh'),
(16, 18, 'xxxxxxx');我现在想通过查询数据库,得到如下表格效果:新闻 eeeerrrrrr
娱乐 ddddddd
eeeeeee
ccccccc
...
电影 sssssss
wwwwww
小说 fffffff
hhhhhhhhA表中的项合并后显示为一行,而不是显示多行。
A表
CREATE TABLE IF NOT EXISTS `A` (
`id` int(11) NOT NULL auto_increment COMMENT 'ID',
`name` char(200) collate utf8_unicode_ci NOT NULL COMMENT '指标名称'
)
INSERT INTO `A` (`id`, `cname`) VALUES
(8, '新闻'),
(18, '娱乐'),
(10, '电影'),
(11, '小说');B表
CREATE TABLE IF NOT EXISTS `B` (
`id` int(11) NOT NULL auto_increment,
`cid` int(11) NOT NULL COMMENT '指标ID',
`name` char(200) NOT NULL COMMENT '名称'
)
INSERT INTO `B` (`id`, `cid`, `name`) VALUES
(6, 18, 'ddddddd'),
(13, 10, 'sssssss'),
(7, 18, 'ccccccc'),
(14, 18, 'eeeeeee'),
(9, 11, 'fffffff'),
(10, 10, 'wwwwww'),
(11, 8, 'eeeerrrrrr'),
(15, 11, 'hhhhhhhh'),
(16, 18, 'xxxxxxx');我现在想通过查询数据库,得到如下表格效果:新闻 eeeerrrrrr
娱乐 ddddddd
eeeeeee
ccccccc
...
电影 sssssss
wwwwww
小说 fffffff
hhhhhhhhA表中的项合并后显示为一行,而不是显示多行。
解决方案 »
- 怎样恢复这个mysqldump文件?
- v9 安装模块出现 1064错误
- 有关mysql中文大数据模糊搜索
- mysql 修改密码时候的那个()是做什么用的
- 关于查询的一点问题,在线等答案……
- MYSQL4.0导入到5.0
- group by 和having count的怪问题
- mysql和sqlsever是什么关系啊? 各位大哥帮帮我
- 出现Fatal error: Maximum execution time of 60 seconds exceeded in .....
- 请问哪儿有完整的mysql参考手册下载呀?
- 请问PostgreSQL定义表的字段长度的时候,如何指定是全角的还是半角的长度?
- window mysql 不能做2个库的增量备份
mysql> select a.cname,group_concat(b.name) from t8 a left join t9 b on a.id=b.cid group by a.cname;
+-------+---------------------------------+
| cname | group_concat(b.name) |
+-------+---------------------------------+
| 娱乐 | ddddddd,xxxxxxx,eeeeeee,ccccccc |
| 小说 | hhhhhhhh,fffffff |
| 新闻 | eeeerrrrrr |
| 电影 | sssssss,wwwwww |
+-------+---------------------------------+
4 rows in setmysql> 是要这样的结果吗?
+-------+---------+
| cname | name |
+-------+---------+
| 娱乐 | ddddddd |
| | xxxxxxx |
| | eeeeeee |
| | ccccccc |
| 小说 | hhhhhhhh|
| | fffffff |
| 新闻 | eeeerrrrrr
| 电影 | sssssss |
| | wwwwww |
+-------+---------------------------------
(CASE WHEN EXISTS(SELECT 1 FROM B WHERE CID=T.CID AND ID>T.ID) THEN '' ELSE A.CNAME END) AS CNAME,
T.NAME
FROM
A,B T
WHERE
A.ID=T.CID/**
CNAME NAME
新闻 eeeerrrrrr
wwwwww
电影 sssssss
fffffff
小说 hhhhhhhh
ddddddd
ccccccc
eeeeeee
娱乐 xxxxxxx
**/