mysql 怎么查询一个字段对应的多个值
展开全部
请参见下列mysql实验:
--
生成orderproduct表
create
table
orderproduct(orderid
char(11)
primary
key,productid
varchar(255));
--
product表
create
table
product(productid
varchar(10)
primary
key,productname
varchar(50));
--
向订单表插入数据
insert
into
orderproduct
values
(20161116001,'d0020'),
(20161116035,'e0055'),
(20161101048,'a0035'),
(20161005321,'b0049'),
(20160901515,'c0038'),
(20160814525,'c0038,a0035,e0055'),
(20160714510,'d0020,b0049');
--
向产品表插入数据
insert
into
product
values
('d0020','立顿牌绿茶'),
('e0055','越南小面包'),
('a0035','珠宝台历'),
('b0049','护手霜'),
('c0038','运动水壶');
select
*
from
orderproduct;
select
*
from
product;
--
返回第一问的sql语句
select
a.orderid,min(a.productid)
as
productid,
group_concat(b.productname)
as
productname
from
orderproduct
a,product
b
where
b.productname
in
('运动水壶','珠宝台历','越南小面包')
and
instr(a.productid,b.productid)>0
group
by
a.orderid;
--
返回第二问的sql语句
select
a.orderid,min(a.productid)
as
productid,
group_concat(b.productname)
as
productname
from
orderproduct
a,product
b
where
b.productname
in
('立顿牌绿茶','护手霜')
and
instr(a.productid,b.productid)>0
group
by
a.orderid;
--
生成orderproduct表
create
table
orderproduct(orderid
char(11)
primary
key,productid
varchar(255));
--
product表
create
table
product(productid
varchar(10)
primary
key,productname
varchar(50));
--
向订单表插入数据
insert
into
orderproduct
values
(20161116001,'d0020'),
(20161116035,'e0055'),
(20161101048,'a0035'),
(20161005321,'b0049'),
(20160901515,'c0038'),
(20160814525,'c0038,a0035,e0055'),
(20160714510,'d0020,b0049');
--
向产品表插入数据
insert
into
product
values
('d0020','立顿牌绿茶'),
('e0055','越南小面包'),
('a0035','珠宝台历'),
('b0049','护手霜'),
('c0038','运动水壶');
select
*
from
orderproduct;
select
*
from
product;
--
返回第一问的sql语句
select
a.orderid,min(a.productid)
as
productid,
group_concat(b.productname)
as
productname
from
orderproduct
a,product
b
where
b.productname
in
('运动水壶','珠宝台历','越南小面包')
and
instr(a.productid,b.productid)>0
group
by
a.orderid;
--
返回第二问的sql语句
select
a.orderid,min(a.productid)
as
productid,
group_concat(b.productname)
as
productname
from
orderproduct
a,product
b
where
b.productname
in
('立顿牌绿茶','护手霜')
and
instr(a.productid,b.productid)>0
group
by
a.orderid;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询