使用 MySQL DISTINCT 消除重复行

发布于 2024-11-26 21:01:53 字数 1255 浏览 0 评论 0原文

我有一个世界数据库。下表是通过以下方式生成的:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

坐在坟头思考人生 2024-12-03 21:01:53

正如马沃所说,这些本身并不是重复的行,而是一些重复的字段。最好的方法可能是重新设计数据库并将该表至少分成两个。这可以作为您的起点:

首先创建一个包含不同唯一城市的表:

CREATE TABLE city SELECT DISTINCT city FROM db

向该表添加主键:

ALTER TABLE city ADD id_city INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST

创建一个包含所有记录以及每个记录所属的相应城市 ID 的表:

CREATE TABLE records SELECT db.id,db.country, db.region, city.id_city, db.lat,db.long FROM db INNER JOIN city ON db.city = city.city

如果您愿意,您可以为与国家/地区相关的城市制作另一张。

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:

CREATE TABLE city SELECT DISTINCT city FROM db

Add a primary key to that table:

ALTER TABLE city ADD id_city INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST

Create a table that contains all your records and the id of the corresponding city that each record belongs to:

CREATE TABLE records SELECT db.id,db.country, db.region, city.id_city, db.lat,db.long FROM db INNER JOIN city ON db.city = city.city

You can make another one for cities asocciated with countries if you wish.

悍妇囚夫 2024-12-03 21:01:53

这个查询有效,我想选择不同的城市、区域组合,并将其与其关联的数据分组到新表中。

CREATE TABLE clean_gls SELECT * FROM geolocations GROUP BY city, region;

This query worked I wanted to select distinct city, region combinations and group it with its associated data in the new table.

CREATE TABLE clean_gls SELECT * FROM geolocations GROUP BY city, region;
独木成林 2024-12-03 21:01:53

在 MSSQL 中是这样的“

SELECT distinct City, Region
INTO tmp
FROM geolocations 
WHERE City = 'Santa Cruz'

在 MYSQL 中,您可以使用 select into table

虽然我还没有测试过,但类似这样的东西应该可以工作:

INSERT INTO tmp(City, Region)
    SELECT distinct City, Region
    FROM geolocations WHERE WHERE City = 'Santa Cruz'

IN MSSQL it's this"

SELECT distinct City, Region
INTO tmp
FROM geolocations 
WHERE City = 'Santa Cruz'

In MYSQL, you can use select into table

Although I havent tested it, something like this should work:

INSERT INTO tmp(City, Region)
    SELECT distinct City, Region
    FROM geolocations WHERE WHERE City = 'Santa Cruz'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文