因此,我编写了一个查询来获取订单(这是针对电子商务类型的网站),并从该订单 ID 获取所有订单项 (ecom_order_items)、打印选项 (c_print_options) 和图像 (images)。 eoi_p_id 当前是 images 表中的外键。
这工作正常,查询是:
SELECT eoi_parentid, eoi_p_id, eoi_po_id, eoi_quantity, i_id, i_parentid, po_name, po_price FROM ecom_order_items, images, c_print_options WHERE eoi_parentid = '1' AND i_id = eoi_p_id AND po_id = eoi_po_id;
以上内容将获取订单 #1 所需的所有内容
现在,为了使事情变得复杂,我添加了一个额外的表(ecom_products),它需要以与图像表类似的方式运行。 eoi_p_id 也可以指向该表中的外键。我添加了一个额外的字段“eoi_type”,其值为“image”或“product”。
现在订单中的商品可以由图像或 ecom_products 中的商品混合组成。无论我尝试什么,它要么最终会产生太多记录,实际上不会输出任何带有 eoi_type = 'product' 的记录,而且通常不会工作。关于如何实现我所追求的目标有什么想法吗?如果需要可以提供SQL示例吗?
SELECT eoi_id, eoi_parentid, eoi_p_id, eoi_po_id, eoi_po_id_2, eoi_quantity, eoi_type, i_id, i_parentid, po_name, po_price, po_id, ep_id FROM ecom_order_items, images, c_print_options, ecom_products WHERE eoi_parentid = '9' AND i_id = eoi_p_id AND po_id = eoi_po_id
上面输出重复的行并且不按预期工作。我是否以错误的方式处理这个问题?我是否应该为 eoi_p_id 有单独的外键字段,具体取决于它是图像还是产品?
我应该使用 JOIN 吗?
这是相关表的 mysql 解释
ecom_products +-------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+----------------+ | ep_id | int(8) | NO | PRI | NULL | auto_increment | | ep_title | varchar(255) | NO | | NULL | | | ep_link | text | NO | | NULL | | | ep_desc | text | NO | | NULL | | | ep_imgdrop | text | NO | | NULL | | | ep_price | decimal(6,2) | NO | | NULL | | | ep_category | varchar(255) | NO | | NULL | | | ep_hide | tinyint(1) | NO | | 0 | | | ep_featured | tinyint(1) | NO | | 0 | | +-------------+--------------+------+-----+---------+----------------+ ecom_order_items +--------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+-------------+------+-----+---------+----------------+ | eoi_id | int(8) | NO | PRI | NULL | auto_increment | | eoi_parentid | int(8) | NO | | NULL | | | eoi_type | varchar(32) | NO | | NULL | | | eoi_p_id | int(8) | NO | | NULL | | | eoi_po_id | int(8) | NO | | NULL | | | eoi_quantity | int(4) | NO | | NULL | | +--------------+-------------+------+-----+---------+----------------+ c_print_options +------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+----------------+ | po_id | int(8) | NO | PRI | NULL | auto_increment | | po_name | varchar(255) | NO | | NULL | | | po_price | decimal(6,2) | NO | | NULL | | +------------+--------------+------+-----+---------+----------------+ images +--------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+----------------+ | i_id | int(8) | NO | PRI | NULL | auto_increment | | i_filename | varchar(255) | NO | | NULL | | | i_data | longtext | NO | | NULL | | | i_parentid | int(8) | NO | | NULL | | +--------------+--------------+------+-----+---------+----------------+
最佳答案
您在 WHERE 或 FROM 子句中缺少 ecom_products 的连接条件。这就是使用 ANSI-92 连接完成的方式
SELECT
eoi_id,
eoi_parentid,
eoi_p_id,
eoi_po_id,
eoi_po_id_2,
eoi_quantity,
eoi_type,
i_id,
i_parentid,
po_name,
po_price,
po_id,
ep_id
FROM
ecom_order_items,
LEFT JOIN images
ON i_id = eoi_p_id
LEFT JOIN c_print_options
ON po_id = eoi_po_id
INNER JOIN ecom_products
ON eoi_p_id = ep_id
WHERE
eoi_parentid = '9'
首选 ANSI 92 连接,它更清楚什么是连接以及什么是过滤。也就是说,您只需将 AND eoi_p_id = ep_id
添加到 where 子句即可。
关于sql - 4表查询/连接。获取重复行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4576358/