考虑下面的SQL查询:
SELECT DISTINCT shops.*,
DA.delivery_cost,
DA.postcode
FROM shops
JOIN shops_delivery_area as DA on DA.shop_id = shops.id
WHERE DA.postcode = "Liverpool"
OR location = "Liverpool"
shops
表
+----+----------+-----------+----------+
| id | name | location | postcode |
+----+----------+-----------+----------+
| 1 | Shop One | Liverpool | L10 |
| 2 | Shop Two | Liverpool | L16 |
+----+----------+-----------+----------+
shops_delivery_area
表
+------------------+---------+----------+---------------+
| delivery_area_id | shop_id | postcode | delivery_cost |
+------------------+---------+----------+---------------+
| 1 | 1 | L10 | 0.00 |
| 2 | 1 | L11 | 0.00 |
| 3 | 1 | L12 | 1.00 |
| 4 | 1 | L13 | 1.00 |
| 5 | 2 | L10 | 0.00 |
| 6 | 2 | L16 | 0.00 |
| 7 | 2 | L28 | 0.00 |
+------------------+---------+----------+---------------+
用户可以通过邮政编码(例如:L14
,L15
,L16
)或文本框中的位置进行搜索。
如果用户输入“利物浦”,它将找到位于“利物浦”的所有商店。问题是会有重复的商店名称(商店表)。如何解决这个问题?
结果(按位置搜索):
如何避免重复商店名称?
我应该这样做:shops.id = shops_delivery_area.shop_id AND shops.postcode = shops_delivery_area.postcode
+----+----------+-----------+----------+---------------+----------+
| id | name | location | postcode | delivery_cost | postcode |
+----+----------+-----------+----------+---------------+----------+
| 1 | Shop One | Liverpool | L10 | 0.00 | L10 |
| 1 | Shop One | Liverpool | L10 | 0.00 | L11 |
| 1 | Shop One | Liverpool | L10 | 1.00 | L12 |
| 1 | Shop One | Liverpool | L10 | 1.00 | L13 |
| 2 | Shop Two | Liverpool | L16 | 0.00 | L10 |
| 2 | Shop Two | Liverpool | L16 | 0.00 | L16 |
| 2 | Shop Two | Liverpool | L16 | 0.00 | L28 |
+----+----------+-----------+----------+---------------+----------+
结果(按邮政编码L10搜索): 按预期正常工作
+----+----------+-----------+----------+---------------+----------+
| id | name | location | postcode | delivery_cost | postcode |
+----+----------+-----------+----------+---------------+----------+
| 1 | Shop One | Liverpool | L10 | 0.00 | L10 |
| 2 | Shop Two | Liverpool | L16 | 0.00 | L10 |
+----+----------+-----------+----------+---------------+----------+
答案 0 :(得分:0)
在“按位置排列的结果”中,您有“delivery_cost”和“postcode”,只要它们不同,您将始终获得“重复”删除这两个,并且您只会看到其中一个。
编辑: 如果您需要这些字段以及商店应该具有的默认值。如果您需要“delivery_cost”和“postcode”的所有结果,但只有一个结果,您应该重新考虑您正在尝试做什么或解释您期望的结果。
答案 1 :(得分:0)
SELECT shops.*,
MIN(DA.delivery_cost),
MIN(DA.postcode)
FROM shops
JOIN shops_delivery_area as DA on
(DA.shop_id = shops.id AND shops.postcode = DA.postcode)
WHERE DA.postcode = "Liverpool"
OR location = "Liverpool"
GROUP BY shops.location, shops.name, shops.id, shops.postcode
答案 2 :(得分:0)
匹配两个表中的邮政编码,在按位置搜索时会为您提供不同的商店名称。
之类的东西SELECT DISTINCT shops.*,
DA.delivery_cost,
DA.postcode
FROM shops
JOIN shops_delivery_area as DA on (DA.shop_id = shops.id and DA.postcode = shops.postcode)
WHERE DA.postcode = "Liverpool"
OR location = "Liverpool"
答案 3 :(得分:0)
我设法让它运转起来。我用UNION来解决这个问题。
SELECT U.* FROM
((SELECT DISTINCT shops.*, DA.delivery_cost, DA.postcode AS AreaPostcode FROM shops
JOIN shops_delivery_area2 as DA on DA.shop_id = shops.id
WHERE DA.postcode = "Liverpool")
UNION
(SELECT DISTINCT shops.*, DA.delivery_cost, DA.postcode AS AreaPostcode FROM shops
JOIN shops_delivery_area2 as DA on DA.shop_id = shops.id AND DA.postcode = shops.postcode
WHERE shops.location = "Liverpool")) as U
如果无论如何要缩短或改进此代码,请告诉我。感谢