环境是 mysql4.1
有两个表
表一是推荐书目列表(R1),其中 RecommendBook存的是推荐书的ID,对应书的表 Book
ID | RecommendBook | RecommendCourse | BuildDate
1  | 1,2,3,4,5,6   | 11,35,6,8,6     | 2008-08-05
2  | 10,2,3,4,5,6  | 1,35,6,8,6      | 2008-08-06
3  | 1,21,3,4,5,6  | 1,351,6,8,6     | 2008-08-07
4  | 1,2,33,4,5,6  | 1,35,61,8,6     | 2008-08-08
5  | 1,2,3,4,5,6   | 1,35,6,81,6     | 2008-08-09下面是书的列表(Book)
ID | Name  | Author
1  | 概论1 | 随意1
2  | 概论2 | 随意2
3  | 概论3 | 随意3
4  | 概论4 | 随意4
5  | 概论5 | 随意5
6  | 概论6 | 随意6我想用一条语句,查出来推荐的都有什么书,课程等,其中书的字段内是所有的  书名,书名 的格式Select R1.ID,(select group_concat(Name,',') from Book where ID in(RecommendBook)) as BookList
from R1
本来想这样可以查出来的
想不到,where id in(字段)不能用
如果我要是把  where ID in(RecommendBook) 换成具体的 where ID in(1,2,3,4,5)就可以了
不知道各位达人有什么好的方法啊?

解决方案 »

  1.   

    Select R1.ID,(select group_concat(Name,',') from Book where 
    instr(concat(',',RecommendBook,','),concat(',',ID,',')>0) as BookList
    from R1 
      

  2.   

    尔……
    楼上的 WWWWA 大侠
    可以说说原理么?
    你写的那个sql不能执行,单独执行子查询的数据也不正确,全成了null了……
    可以告诉我你这么写的原理么?
      

  3.   

    select id,group_concat(name order by name) from (
    select a.*,b.Name from r1 a
    left join book b
    on instr(concat(',',a.RecommendBook,','),concat(',',b.ID,','))>0) c
    group by id
      

  4.   

    select id,group_concat(name order by name) from ( 
    select a.*,b.Name from r1 a 
    left join book b 
    on instr(concat(',',a.RecommendBook,','),concat(',',b.ID,','))>0) c 
    group by id
      

  5.   

    Select R1.ID,(select group_concat(name SEPARATOR ",") from Book where find_in_set(ID,R1.RecommendBook)) as BookList 
    from R1
      

  6.   

    简化:
    Select R1.ID,(select group_concat(Name) from Book 
    where concat('%,',RecommendBook,',%') like concat('%,',ID,',%')) as BookList
    from R1 
      

  7.   

    你的RecommendBook是用逗号为分隔符,用find_in_set也可以
      

  8.   

    我忽然发现,yyz0832 的方法很好用
    效率也不错
    谢谢
    也同时谢谢 WWWWA  这么热心的回复
    谢谢大家
      

  9.   

    Select R1.ID,(select group_concat(Name,',') from Book where
    instr(concat(',',RecommendBook,','),concat(',',ID,',')>0) as BookList
    from R1 ->Select R1.ID,(select group_concat(Name,',') from Book where
    instr(concat(',',RecommendBook,','),concat(',',ID,','))>0) as BookList
    from R1 呵呵,少了一个括号