SQL返回重复的值

时间:2017-09-20 12:04:49

标签: mysql sql

我有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

There is a screenshot of my result

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

2 个答案:

答案 0 :(得分:0)

根据屏幕截图和您的评论,您只需要查询中的两个值,其中id193的费率为21,而1539的费率为0,我猜测您要么:

  1. 缺少ps_bestkit_gift_rule与一个或多个其他表之间的连接条件。
  2. 缺少ps_bestkit_gift_rule本身的限制条件。
  3. 缺少ps_tax上的加入条件最有可能在重新审核问题后。我最初没有考虑到ps_beskit_gift_rule上的左连接。
  4. 缺少对ps_tax的限制条件
  5. +------------+----------+--------------------+--------+--------+------------+
    | 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!

答案 1 :(得分: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