使用 MySQL DISTINCT 消除重复行
我有一个世界数据库。下表是通过以下方式生成的:
SELECT *
FROM geolocations
WHERE city = 'Santa Cruz'
该表包含每个城市的多个条目,它曾经包含一个邮政编码字段。 我想删除城市的重复条目。我尝试使用:
CREATE TABLE tmp
SELECT DISTINCT city, region
FROM geolocations
上一个语句消除了重复的条目,但是如何将 id 和其他字段复制到新表中?
-----------------------------------------------------------------------
id Country Region City Latitutde Longitude
-----------------------------------------------------------------------
4683 US CA Santa Cruz 37.0447998047 -122.1020965576
5748 US CA Santa Cruz 36.9712982178 -121.9875030518
9506 US CA Santa Cruz 37.0101013184 -122.0324020386
11205 US CA Santa Cruz 37.0344009399 -121.9796981812
11379 US CA Santa Cruz 36.9898986816 -122.0603027344
13146 US CA Santa Cruz 37.0101013184 -122.0324020386
14362 US CA Santa Cruz 37.0101013184 -122.0324020386
30055 BO 03 Santa Cruz -12.2833003998 -66.2500000000
31760 ES 59 Santa Cruz 42.6666984558 -2.3499999046
39477 AR 22 Santa Cruz -27.6667003632 -64.2667007446
-----------------------------------------------------------------------
I have a world db. The table below was generated with:
SELECT *
FROM geolocations
WHERE city = 'Santa Cruz'
The table contains multiple entries for each city, it once contained a Zip Code field.
I want to delete duplicate entries for cities. I tried using:
CREATE TABLE tmp
SELECT DISTINCT city, region
FROM geolocations
The previous statement eliminates the duplicate entries, but how do I get id and other fields to copy to new table?
-----------------------------------------------------------------------
id Country Region City Latitutde Longitude
-----------------------------------------------------------------------
4683 US CA Santa Cruz 37.0447998047 -122.1020965576
5748 US CA Santa Cruz 36.9712982178 -121.9875030518
9506 US CA Santa Cruz 37.0101013184 -122.0324020386
11205 US CA Santa Cruz 37.0344009399 -121.9796981812
11379 US CA Santa Cruz 36.9898986816 -122.0603027344
13146 US CA Santa Cruz 37.0101013184 -122.0324020386
14362 US CA Santa Cruz 37.0101013184 -122.0324020386
30055 BO 03 Santa Cruz -12.2833003998 -66.2500000000
31760 ES 59 Santa Cruz 42.6666984558 -2.3499999046
39477 AR 22 Santa Cruz -27.6667003632 -64.2667007446
-----------------------------------------------------------------------
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如马沃所说,这些本身并不是重复的行,而是一些重复的字段。最好的方法可能是重新设计数据库并将该表至少分成两个。这可以作为您的起点:
首先创建一个包含不同唯一城市的表:
向该表添加主键:
创建一个包含所有记录以及每个记录所属的相应城市 ID 的表:
如果您愿意,您可以为与国家/地区相关的城市制作另一张。
As Marvo said those are not duplicate rows perse but some fields duplicated. The best approach could be to redesign your database and break that table into two -at least-. This could serve you as a starting point:
First create a table that contains different-uniques- cities:
Add a primary key to that table:
Create a table that contains all your records and the id of the corresponding city that each record belongs to:
You can make another one for cities asocciated with countries if you wish.
这个查询有效,我想选择不同的城市、区域组合,并将其与其关联的数据分组到新表中。
This query worked I wanted to select distinct city, region combinations and group it with its associated data in the new table.
在 MSSQL 中是这样的“
在 MYSQL 中,您可以使用 select into table
虽然我还没有测试过,但类似这样的东西应该可以工作:
IN MSSQL it's this"
In MYSQL, you can use select into table
Although I havent tested it, something like this should work: