非常好奇的问题。当我放弃“国家”时,它似乎解决了这个问题。
SELECT DISTINCT PostalCode, City, Region
总共 128 个,查询耗时 0.0066 秒
SELECT DISTINCT PostalCode, City, Region, Country
共 209 个,查询耗时 0.0002 秒
此外,该行为似乎只影响ImportCustomers
and ImportOrders
:
SELECT postalcode, city, region, country
FROM
(SELECT postalcode, city, region, country FROM importcustomers
UNION
SELECT shippostalcode, shipcity, shipregion, shipcountry FROM importorders) t
总共 172 个,查询耗时 0.0053 秒
SELECT postalcode
FROM
(SELECT postalcode FROM importcustomers
UNION
SELECT shippostalcode FROM importorders) t
总共 91 个,查询耗时 0.0050 秒
然后我将其缩小到country
列importcusotmers
importorders
SELECT TRIM(country) AS country FROM importcustomers
UNION
SELECT TRIM(shipcountry) AS country FROM importorders
阿根廷
阿根廷
奥地利
奥地利
比利时
比利时
...
当我将专栏投到BINARY
SELECT BINARY country AS country FROM importcustomers
UNION
SELECT BINARY shipcountry AS country FROM importorders
阿根廷
417267656e74696e610d
奥地利
417573747269610d
比利时
42656c6769756d0d
...
该表ImportOrders
导致重复。
SELECT BINARY shipcountry AS country FROM importorders
4765726d616e790d
5553410d
5553410d
4765726d616e790d
...
查看您提供的转储,国家末尾附加了一个额外的\r
(在值中表示为)。0d
--
-- 转储表 `importorders` 的数据
--
插入“进口订单”值
...'德国\r'),
...'美国\r'),
...'美国\r'),
...'德国\r'),
...'墨西哥\r'),
importcustomers
在哪里country
看起来不错:
--
-- 转储表 `importcustomers` 的数据
--
插入“importcustomers”值
...'德国', ... ,
...'墨西哥', ... ,
...'墨西哥', ... ,
...'英国', ... ,
...'瑞典',...,
您可以通过运行以下查询来删除这些\r
(回车):
UPDATE importorders SET ShipCountry = REPLACE(ShipCountry, '\r', '')
然后,如果您运行原始查询,您将获得所需的结果集。仅供参考,DISTINCT
如果您使用的是UNION
.
SELECT PostalCode, City, Region, Country
FROM
(SELECT postalcode, city, region, country FROM importemployees
UNION
SELECT postalcode, city, region, country FROM importcustomers
UNION
SELECT postalcode, city, region, country FROM importproducts
UNION
SELECT shippostalcode as postalcode, shipcity as city,
shipregion as region, shipcountry as country FROM importorders) T