繁体   English   中英

SQL返回重复的值

[英]SQL returns duplicated values

我在使用SQL查询时遇到问题,因为它返回多行,我只想从中获取值

ps_tax 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

根据屏幕截图和您的评论,您只想从查询中删除两个值,即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和rate都具有不同的值,毕竟我可能看错了表... gift_price的NULL可能是左连接的结果,这意味着ps_tax中ID_TAX和RATE的两个值可能是表有不正确的连接! 我来看看那张桌子上的PK / FK!

我认为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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM