求一条sql语句:
图书表与图书类型表如下:(mysql数据库)
[code="sql"]
create table book(
id int primary key auto_increment,
bookTypes varchar(60),--图书属于的类型,一本图书对应多个图书类型用逗号隔开
name varchar(60) not null
);
create table bookType(
id int primary key auto_increment,
name varchar(40) not null
);
insert into bookType values(null,'A');
insert into bookType values(null,'B');
insert into bookType values(null,'C');
insert into bookType values(null,'D');
insert into book values(null,'1,2,4','android');
insert into book values(null,'1,3','java');[/code]
现在给出一个图书类型的id号码,如何用一条sql 语句查出属于这个图书类型的所有图书?
select * from book where booktypes 包含给出的图书类型id;
select * from book where booktypes like '%1%'
比如:图书类型id为1,图书类型为B
[color=red]现在给出一个图书类型的id号码[/color]
你说的是不是添加图书类型B,就是下面的sql,
select * from book where booktypes like (select auto_increment from bookType where name='B')
你说得要是提供图示类型id为1,sql为
select * from book where booktypes like '1'