SQL 返回重复值

问题描述 投票:0回答:2

我的 SQL 查询有问题,因为它返回多行,我希望仅从

获取值

ps_税t

有人可以帮助我吗?

SELECT bgc.id_product,bgc.id_order, tx.id_tax_rules_group,t.id_tax,pl.name,t.rate as rate, brl.price as gift_price
FROM ps_bestkit_gift_cart bgc
LEFT JOIN ps_bestkit_gift_rule brl ON brl.id_bestkit_gift_rule = bgc.id_bestkit_gift_rule
LEFT JOIN ps_product_lang pl ON bgc.id_product = pl.id_product
LEFT JOIN ps_product p ON pl.id_product = p.id_product
LEFT JOIN ps_tax_rule tx ON p.id_tax_rules_group = tx.id_tax_rules_group
LEFT JOIN ps_tax t ON tx.id_tax = t.id_tax
WHERE id_order =8452
AND pl.id_lang=3
AND tx.id_country=6

有我的结果截图

DDL

CREATE TABLE ps_bestkit_gift_rule (
 id_bestkit_gift_rule int(11) unsigned NOT NULL AUTO_INCREMENT,
 is_product_depends tinyint(1) unsigned DEFAULT NULL,
 is_supplier_depends tinyint(1) unsigned DEFAULT NULL,
 is_manufacturer_depends tinyint(1) unsigned DEFAULT NULL,
 is_category_depends tinyint(1) unsigned DEFAULT NULL,
 is_attribute_depends tinyint(1) unsigned DEFAULT NULL,
 is_feature_depends tinyint(1) unsigned DEFAULT NULL,
 is_allow_other_gift tinyint(1) unsigned DEFAULT NULL,
 cart_amount decimal(17,2) unsigned NOT NULL,
 max_cart_amount decimal(17,2) unsigned NOT NULL,
 price decimal(17,2) unsigned NOT NULL,
 min_qty_inside_category int(10) unsigned NOT NULL DEFAULT 0,
 min_price_inside_category decimal(17,2) unsigned NOT NULL,
 from_date date NOT NULL,
 to_date date NOT NULL,
 criteria_order_type varchar(32) NOT NULL,
 criteria_max_per_customer int(11) unsigned NOT NULL,
 criteria_nb_products int(11) unsigned NOT NULL,
 gift_preselector_product_page tinyint(1) unsigned DEFAULT NULL,
 available_gifts int(10) unsigned NOT NULL DEFAULT 1,
 criteria_coupon text,
 criteria_customer_group text,
 message tinyint(1) unsigned DEFAULT NULL,
 active tinyint(1) unsigned DEFAULT NULL,
 position int(10) unsigned NOT NULL DEFAULT 0,
 PRIMARY KEY (id_bestkit_gift_rule)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8

mysql sql
2个回答
1
投票

根据屏幕截图和您的评论,您只需要查询中的两个值,即 id193,速率 21 和 1539,速率 0 我猜您是:

  1. ps_bestkit_gift_rule 与一个或多个其他表之间缺少连接条件。
  2. 缺少 ps_bestkit_gift_rule 本身的限制标准。
  3. 缺少 ps_tax 的加入标准 最有可能是在重新审查问题后。 我最初没有考虑 ps_beskit_gift_rule 上的左连接。
  4. 缺少 ps_tax 的限制标准。
+------------+----------+--------------------+--------+--------+------------+
| ID_Product | ID_order | ID_TAX_RULES_GROUP | ID_TAX |  Rate  | Gift_price |
+------------+----------+--------------------+--------+--------+------------+
|        193 |     8452 |                  4 |      4 | 21.000 |  $5.00     |
|       1539 |     8452 |                  4 |      4 | 21.000 |  $-        |
|        193 |     8452 |                  4 |      7 |  0.000 |  $5.00     |
|       1539 |     8452 |                  4 |      7 |  0.000 |  $-        |
+------------+----------+--------------------+--------+--------+------------+

因此 ID_TaX 和 Gift_price 以及费率都有不同的值,毕竟我可能正在查看错误的表...gift_price 上的 NULL 可能是左连接的结果,这意味着 ps_tax 中的 ID_TAX 和 RATE 的两个值可能是该表有不正确的连接! 我会看看那张桌子上的 PK/FK!


0
投票

我认为 GROUP BY 可以完成这项工作。

SELECT bgc.id_product,bgc.id_order,
tx.id_tax_rules_group,t.id_tax,pl.name,t.rate as rate, brl.price as gift_price 
FROM ps_bestkit_gift_cart bgc
LEFT JOIN ps_bestkit_gift_rule brl ON brl.id_bestkit_gift_rule = bgc.id_bestkit_gift_rule 
LEFT JOIN ps_product_lang pl ON bgc.id_product = pl.id_product 
LEFT JOIN ps_product p ON pl.id_product = p.id_product 
LEFT JOIN ps_tax_rule tx ON p.id_tax_rules_group = tx.id_tax_rules_group 
LEFT JOIN ps_tax t ON tx.id_tax = t.id_tax 
WHERE id_order =8452 
AND pl.id_lang=3 
AND tx.id_country=6
GROUP BY t.id_tax
© www.soinside.com 2019 - 2024. All rights reserved.