我定制OpenCart 3
的方式,客户能够按照category
和manufacturer
。
在主页上,每个客户都会看到一个基于他/她关注的产品列表。
为了节省数据大小,我将 category 和 customer 缩写为c
and m
,这对我进行连接查询来说是一个大问题。
另一方面,我想先加载一个列表 product_ids 订单date_modified
,然后在scrolling down
,按要求加载完整的产品信息。
更新的小提琴
SqlFiddle:http : //sqlfiddle.com/#! 9/831301/2
CREATE TABLE follow (
`customer_id` int(11) NOT NULL,
`item_id` int(11) NOT NULL,
`item_type` varchar(1) NOT NULL,
PRIMARY KEY (`customer_id`,`item_id`,`item_type`)
)
INSERT INTO follow (customer_id, item_id, item_type) VALUES
(1, 1, 'm'),
(1, 2, 'm'),
(1, 3, 'm'),
(1, 1, 'c'),
(1, 2, 'c'),
(1, 3, 'c');
-- `m` stands for `manufacturer`
-- 'c' stands for `category`
CREATE TABLE IF NOT EXISTS `product` (
`product_id` int(11) NOT NULL AUTO_INCREMENT,
`price` decimal(15,4) NOT NULL DEFAULT '0.0000',
`manufacturer_id` int(11) NOT NULL,
`date_added` datetime NOT NULL,
`date_modified` datetime NOT NULL,
PRIMARY KEY (`product_id`)
)
CREATE TABLE IF NOT EXISTS `product_description` (
`product_id` int(11) NOT NULL,
`language_id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`product_id`,`language_id`),
KEY `name` (`name`)
)
CREATE TABLE IF NOT EXISTS `category` (
`category_id` int(11) NOT NULL AUTO_INCREMENT,
`parent_id` int(11) NOT NULL DEFAULT '0',
`top` tinyint(1) NOT NULL,
PRIMARY KEY (`category_id`,`parent_id`),
KEY `parent_id` (`parent_id`)
)
CREATE TABLE IF NOT EXISTS `manufacturer` (
`manufacturer_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(64) NOT NULL,
PRIMARY KEY (`manufacturer_id`)
)
为此,我试图让自己更简单,但结果是错误的:
更新的查询
SELECT DISTINCT p.product_id, p.price, procats.category_id FROM product p
LEFT JOIN
(SELECT DISTINCT pc.product_id, pc.category_id FROM follow f2
LEFT JOIN product_to_category pc on (f2.item_id = pc.category_id)
WHERE f2.item_type = 'c') AS procats ON (procats.product_id = p.product_id)
order by p.price
更新:我的查询结果:
product_id price category_id
9 15 (null)
1 15 1
1 15 2
1 15 3
2 15 1
3 15 2
4 15 (null)
5 15 3
6 15 (null)
7 15 (null)
8 15 (null)
此外,如果您就改进整个结构和纠正可能的错误向我提出任何建议,我将不胜感激。
而不是两个关注表,一个用于制造商,一个用于类别,您只决定了一个,这意味着您的关注表指向您称为项目的“事物”,它可以是制造商或类别,并且 DBMS 看不到什么您实际上指向并帮助您进行一致性检查。
现在您需要客户通过制造商或类别关注的产品列表。因此,选择product
并使用IN
或EXISTS
将其限制为客户关注的产品:
select *
from product
where manufacturer_id in
(
select item_id from follow where item_type = 'm' and customer_id = 1
)
or product_id in
(
select product_id from product_to_category where category_id in
(select item_id from follow where item_type = 'c' and customer_id = 1)
)
order by date_modified desc;
SQL小提琴:http ://sqlfiddle.com/#! 9/831301/11
如果您想要带有类别的产品:
select *
from product p
join product_to_category pc on pc.product_id = p.product_id
join category c on c.category_id = pc.category_id
where p.manufacturer_id in
(
select item_id from follow where item_type = 'm' and customer_id = 1
)
or c.category_id in
(
select item_id from follow where item_type = 'c' and customer_id = 1
)
order by date_modified desc;
SQL小提琴:http ://sqlfiddle.com/#! 9/831301/17
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句