Eliminate duplicate rows using MySQL DISTINCT - mysql

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

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.

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;

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'

Related

MySQL update table with data from another table based on field

I have a database that looks like this
leads
name zip city county state
Bill 84058
Susan 90001
FullUSZipCodes
ZipCode City County State
84058 Orem Utah Utah
90001 Los Angeles South California
As you can see, in the leads database the city, count and state are empty, i'm trying to merge the data so that the first table would look like this:
leads
name zip city county state
Bill 84058 Orem Utah Utah
Susan 90001 Los Angeles South California
This is the first query I tried:
UPDATE leads SET leads.county = FullUSZipCodes.County WHERE leads.zip = FullUSZipCodes.ZipCode
which didn't work, and here is the 2nd query:
UPDATE leads INNER JOIN FullUSZipCodes ON leads.zip = FullUSZipCodes.ZipCode SET leads.county = FullUSZipCodes.County, leads.city = FullUSZipCodes.City, leads.state = FullUSZipCodes.State
Your second query looks like it should work, but I would write it slightly differently:
UPDATE leads t1
INNER JOIN FullUSZipCodes t2
ON t1.zip = t2.ZipCode
SET
t1.city = t2.City,
t1.county = t2.County,
t1.state = t2.State
WHERE
t1.city IS NULL OR t1.county IS NULL OR t1.state IS NULL;
If the leads table has a lot of records, and performance is a concern, you can try adding a WHERE clause to the update query which targets only those records which are missing one or more pieces of address information. I think there is nothing wrong with always updating all three columns together, because typically this information tends to be grouped together.

Putting one line per name using SQL

I have this sql output that gives me different row for the same person based on their location. I wanted one line per person and three columns with a Y if they lived there.
Select name, Paris, London, NYC from location
Name Paris London NYC
John y
John y
John y
I want this
Name Paris London NYc
John y y y
you can use max function :
select name, max(paris), max(london), max(nyc) from location
group by name
SELECT name,
IF(SUM(IF(Paris='y',1,0)>0,'y','') as Paris,
IF(SUM(IF(London='y',1,0)>0,'y','') as London,
IF(SUM(IF(NYC='y',1,0)>0,'y','') as NYC
FROM location
GROUP BY name
Side-note, the database is not designed optimally! 3 tables with Name, Cities and Location with proper joins would be much more efficient.
Use Subselects like:
Select name,
(SELECT Paris FROM location WHERE name = a.name),
(SELECT London FROM location WHERE name = a.name)
FROM location a
This way you make a Select in an Select, and link the subselect to the name of the overlaying select.
Some of the other users told you to use aggregations.
In the most database systems an aggregation query is less efficient than a subquery, so you should use aggregations with care.

MySQL Householding: Group Duplicate Addresses And Update Names to Include Both People

If we assume that I already have a query that presented me with the data I want in a very simple way for a mailer: Name, Address, City, State, Zip. But if the file's around 20k, there are a decent amount of people who have the exact same address.
Obviously it's simple to do a GROUP BY Address to remove any duplicates. But the problem I'm running into is not being able to update the names to include both people for a mailer.
If this is two rows in the original data:
Name Address City State Zip
Jerry Seinfeld 129 West 81st Street New York NY 10024
Elaine Benes 129 West 81st Street New York NY 10024
I would like the query output to result in one row where the name looks like this:
Name Address City State Zip
Jerry Seinfeld & Elaine Benes 129 West 81st Street New York NY 10024
And if we want to get fancier, it would be great if they also had the same last name to include something for that in the script so that if both of their last names were "Seinfeld," the output would be:
Name Address City State Zip
Jerry & Elaine Seinfeld 129 West 81st Street New York NY 10024
So generally, I'm just looking for some SELECT *-type query that households these addresses but also includes something to update the names. Thanks!
Below Query would work well, Self tested:
SELECT
IF(COUNT(T1.Address) = (SELECT COUNT(DISTINCT T2.Last) FROM t1 T2 WHERE T2.Address = T1.Address), GROUP_CONCAT(T1.First," ",T1.Last SEPARATOR ' & '), CONCAT(GROUP_CONCAT(T1.First SEPARATOR ' & '), ' ',T1.Last)) Name
FROM t1 T1
GROUP BY T1.Address
GROUP_CONCAT()
Examples here: https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

MySQL Query to display records from table

I have a query.I have table with two columns country and state.I want to display columns in following format
Country State
----------- ---------
India Delhi
Bangalore
Kolkata
Mumbai
USA California
Florida
Las Vegas
Virginia
It means "India" just appear one time in country column and and repeated values would come as blank value in country column when i select country and state from table.
Thanks in advance
Presentation is usually if not always better done outside of SQL so I'd recommend doing this in whatever your presentation layer runs, but if it's a requirement for the query, you can do it using session variables;
SELECT Country, State FROM (
SELECT IF(Country=#country, '', Country) Country, State, #country := Country
FROM (SELECT Country, State FROM Table1 ORDER BY Country, State) dummy1,
(SELECT #country:='') dummy2
) dummy3;
An SQLfiddle to test with.
Just to show a (probably) better way, you can use this to get a list of states per country, and process it further in your presentation layer;
SELECT Country, GROUP_CONCAT(State) FROM Table1 GROUP BY Country;
Another SQLfiddle.
use pl/sql.Moreover your table would be voilating 5th normal form.

Sql Default Country, City, Town lists

i want to bring default country default city default town in every db grid in my admin panel
these are not drop down these are db grid
i have three table
country table
id(int),name(varchar),is_default enum('1','0')
city table
id(int),country_id(int),name(varchar),is_default enum('1','0')
town table
id(int),countr_id(int),city_id(int),name(varchar),is_default enum('1','0')
i have 3 link in my admin panel
Country list
City List
Town List
Country List
- when i open country list it will bring list but default country will come first
country default
USA yes (its coming at first line because its is_dfault=1)
Germany No
thats ok
Select * from country where is_default='1'
City List
When i open that list
City list will come with own county name, but default country will come at first line
and default city will be first in this default country in city list page
Select city.*,country.name as country
left join country on country.id=city.id
order by country.is_default,city.is_default asc
thats wrong sql i know
example
city name country default
new jersey USA yes (this city coming at first line because its is_default=1)
chicago USA no
koln Germany no (Germany cities starting after USA cities because country is default=1)
Town List
is_default value 1 town will come at first with own city towns at first line
example
town name city name country default
usa town newjersey USA 1 (this town coming at first line because its is_default=1)
after usa city towns
germany town1 koln GERMANY 0 (Germany or other country cities start after USA towns)
i think my problem is with order (order by is_default ) or i need diffirent sql with other selections
i tried some codes but countries coming mix and others city and towns list too
waiting your helps.
thanks
I have no idea whether i got what you mean, and I try to give an answer.
I think what mix the countries or other column is wrong join condition.
Your sql here:
Select city.*,country.name as country
left join country on country.id=city.id
order by country.is_default,city.is_default asc
Above is what you post, Because it is not complete, I guess your join condition "on country.id=city.id" is wrong. If you wanna combine the two table, on its contry id, you should
SELECT city.*, county.name AS country_name from
country LEFT JOIN city ON country.id = city.country_id
ORDER BY country.is_default, city.is_default DESC
Because "is_default" is 1, I use "DESC", then default rows whill pop first.
I hope that will be helpful for you :)