有2个表 栏目表C 和 文章表A,C表中pid为父级id,A表中cid为 栏目id,如下:表C
id name pid
1 a 0
2 a-1 0-1
3 a-2 0-1
4 a-2-1 0-1-3表A
id title cid
1 t_a-1 0-1-2
2 t_a-2-1 0-1-3-4 现在想做个联合查询
select a.title, c.name from 2表 where a.cid=(c.pid+"-"+c.id) and a.id=...这样为什么查不到结果呢
id name pid
1 a 0
2 a-1 0-1
3 a-2 0-1
4 a-2-1 0-1-3表A
id title cid
1 t_a-1 0-1-2
2 t_a-2-1 0-1-3-4 现在想做个联合查询
select a.title, c.name from 2表 where a.cid=(c.pid+"-"+c.id) and a.id=...这样为什么查不到结果呢
要用 concatMySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
id----------name----------pid
1----------- a-------------- 0(一级栏目a)
2---------- a-1------------- 0-1(a的子栏目)
3-----------a-2------------- 0-1(a的子栏目)
4-----------a-2-1---------- 0-1-3(a-2的子栏目)表A
id---------title-------------cid
1----------t1------------0-1-2 (a-2栏目下的文章t1)
2----------t2------------0-1-3-4(a-2-1栏目下的文章t2)想达到的查询结果是
a.title------a.cid-------------c.name(文章标题 所属栏目id 栏目名称)
t1 0-1-2 a-2这样是否明白了 我的意思
id---------title-------------cid
1----------t1------------0-1-2 (a-1栏目下的文章t1)
2----------t2------------0-1-3-4(a-2-1栏目下的文章t2)
写错了