在 MySQL 中,如何使用基于另一个 table 的列值的名称加入 table?

In MySQL how to join tables using their names which are based on column values of another table?

我以客户能够关注 categorymanufacturer 的方式自定义了 OpenCart 3
在主页中,每个客户都会看到一个基于 he/she 关注的产品列表。
为了节省数据大小,我使用了类别和客户的缩写 cm,这对我进行连接查询来说是个大问题。
另一方面,我想先加载 product_ids 订单 date_modified 的列表,然后在 scrolling down 时,按要求加载完整的产品信息。

已更新FIDDLE
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)

此外,如果您能给我任何关于改进整个结构和纠正可能的错误的建议,我将不胜感激。

不要在您正在使用 WHERE f2.item_type = 'c' 的 where 条件(用作内部联接)中使用与左联接相关的列 table .. 在 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

您决定只关注一个,而不是两个关注 table,一个针对制造商,一个针对类别,这意味着您的关注 table 指向一个 "thing" 您call item,可以是制造商也可以是品类,DBMS看不到你实际指的是什么,帮你做一致性检查。

现在您需要一个客户通过制造商或类别关注的产品列表。因此 product 中的 select 并使用 INEXISTS 将其限制为客户关注的产品:

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 fiddle: 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 fiddle: http://sqlfiddle.com/#!9/831301/17