mysql选择产品和功能,MySQL从3个表中选择不同的产品

MySQL select from 3 tables.

I have these 5 tables:

CREATE TABLE `category` (

`c_id` int(6) NOT NULL AUTO_INCREMENT,

`name` varchar(40) NOT NULL,

PRIMARY KEY (c_id)

);

CREATE TABLE `product` (

`p_id` int(11) NOT NULL AUTO_INCREMENT,

`name` varchar(40) NOT NULL,

`brand` varchar(30) NOT NULL,

`image_path` varchar(100) DEFAULT NULL,

PRIMARY KEY (p_id)

);

CREATE TABLE `shop` (

`s_id` int(6) NOT NULL AUTO_INCREMENT,

`name` varchar(50) NOT NULL,

`country` varchar(30) NOT NULL,

`province` varchar(30) NOT NULL,

`city` varchar(30) NOT NULL,

`suburb` varchar(30) NOT NULL,

`street` varchar(40) DEFAULT NULL,

`streetNumber` varchar(40) DEFAULT NULL,

`postalCode` int(4) DEFAULT NULL,

PRIMARY KEY (s_id)

) ;

CREATE TABLE product_category (

p_id INT NOT NULL,

c_id INT NOT NULL,

PRIMARY KEY (p_id, c_id),

FOREIGN KEY (p_id) REFERENCES Product(p_id) ON UPDATE CASCADE,

FOREIGN KEY (c_id) REFERENCES Category(c_id) ON UPDATE CASCADE

);

CREATE TABLE product_shop (

p_id INT NOT NULL,

s_id INT NOT NULL,

PRIMARY KEY (p_id, s_id),

FOREIGN KEY (p_id) REFERENCES product(p_id) ON UPDATE CASCADE,

FOREIGN KEY (s_id) REFERENCES shop(s_id) ON UPDATE CASCADE

);

Basically, a product can have many categories. A category can be assigned to many products. A shop can have many products. A product can be in many shops.

I would like to select all products where the category.c_id = 2, or category.c_id = 8 and the shop.s_id = 1 or shop.s_id = 2.

I am part way there with this:

select *

from product inner join product_category

on product_category.p_id=product.p_id

where (product_category.c_id=2)

or (product_category.c_id=8)

That gets all the products that have a category id of 2 and also products with a category id of 8, but it gets the same product twice if it has both category.c_id = 8 and category.c_id = 2.

Then I tried this to make it get unique products:

select DISTINCT(product.p_id) as product

from product inner join product_category

on product_category.p_id=product.p_id

where (product_category.c_id=2)

or (product_category.c_id=8)

Which is now distinct but does not show enough information about the product or category. I want to show as much information as possible in each row.

And the the next step is to only get the ones where the shop.s_id = 1 or shop.s_id = 2.

Can anyone help me get there or get closer? Thanks!

解决方案

Let's say that you want to list all product information. If you dont want the products to repeat, you may use IN clause.

select p.*

from product p

where p.p_id in (select c.p_id from product_category c where c.c_id in (2,8))

and p.p_id in (select s.p_id from product_shop s where s.s_id in (1,2))

Now, if you want all product data and list which categories and shops the product is related to, then you may use join and some very handy functions.

select p.p_id, p.`name`, p.brand, GROUP_CONCAT(DISTINCT c.c_id SEPARATOR ', ') as categories, GROUP_CONCAT(DISTINCT s.s_id SEPARATOR ', ') as shops

from product p inner join product_category c on p.p_id = c.p_id

inner join product_shop s on p.p_id = s.p_id

where c.c_id in (2,8)

and s.s_id in (1,2)

group by p.p_id, p.`name`, p.brand

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值