![](/img/trans.png)
[英]MySQL join for two tables based on on column and excluding the values of another column
[英]In MySQL how to join tables using their names which are based on column values of another table?
我已經定制OpenCart 3
,使客戶能夠關注category
和manufacturer
。
在主頁上,每個客戶將根據其關注的內容看到產品列表。
為了節省數據量,我使用category和customer的縮寫c
和m
,這對於我進行聯接查詢是一個大問題。
另一方面,我想date_modified
加載product_ids列表,然后在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
不要在您使用WHERE f2.item_type = 'c'
條件(用作內部聯接)中使用與左聯接表相關的列..在ON子句中移動此條件
SELECT DISTINCT
p.product_id
, p.model
, 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
AND f2.item_type = 'c'
) AS procats ON procats.product_id = p.product_id
order by p.model
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.