SQL - Finding Duplicates of One Column But Different in Another Column - mysql

How do I find rows that have duplicate values in one column but different values in another column. I have a database of cities with latitude and longitude. I would like to find all rows that have a duplicate in the city name column but have different latitude and longitude. For example, paris,france and paris,texas have the same city name "paris" but different latitude and longitude, so they would both be included in the query results. But it should not return rows that have duplicate city names and duplicate latitude/longitude values.

If you just want the city names:
select city
from table t
group by city
having min(latitude) <> max(latitude) or
min(longitude) <> max(longitude);
If you want the details, then you can join this back to the main table or use group_concat() to assemble a list of countries (for instance).

select city,latitude,longitude,count(*) cnt
from table
group by city,latitude,longitude
having count(*)>1
the cities which come in above resultset are duplicates.

Related

How do I get distinct value in one row and their respective average values as output in mySQL

I have a mySQL table with 100 rows and 6 columns namely ; full_name, name, score, city, gender, rating. I want the output as one column containing distinct city values (there are only 5 distinct cities initially & the user input value of his/her city will be added, namely; Delhi, Mumbai, Patna, Chennai ,Pune) and the second column having their respective avg score.
The database is linked to the python code which I am working on & use takes input which is stored in the above 6 columns. Then according to the user request, the output as analysis is showed as graphs using matplotlib. But I am really stuck at this part where I need to show a graph having X-VALUES as city names and Y-VALUES as respective avg score for that, I need to know the query to get such an output in mySQL where we get 2 columns storing the above.
How do I do it ?
SELECT city AS X,AVG(score) AS Y
FROM yourtable GROUP BY city
Is this, what you ment? Or if you want the result as one row, you add GROUP_CONCAT:
SELECT GROUP_CONCAT(X) AS gX,GROUP_CONCAT(Y) AS gY FROM
(SELECT city AS X,AVG(score) AS Y
FROM yourtable GROUP BY city) g
ok, redbull helps...
correct syntax :
select city, avg(score) from data group by city;
wrong syntax (what I was trying to do earlier) :
select data.distinct(city), data.avg(score) , check.check_city from data, (select name, distinct(city) as check_city from data) check where data.name = check.name and data.city in check.check_city;
Thanks Anyway !

SQL QUERY - How to extract last child of a table?

I have a MYSQL table of groups of people organised by country, region, sub region and city.
When visitor join a group, he select a city and we automatically add him in the parents groups "sub-region", "region, "country".
For example: John selected London. So he will be added in groups London, Greater London, England, UK.
We get a parent-child table like this:
http://prntscr.com/3kui69
I need to extract all the rows for the city groups. How to recognise a city groups? It is the only rows where its ID is not in id_parent field of other rows.
Yes! City groups rows can't be parents of other groups. So we can't find city groups id in id_parent fields.
Now that we know his, how can I extract the city groups rows with SQL language? It is too complicate for me.
Thanks in advance.
Please try using this query:
select * from table
where id not in (select id_parent from table);
Here is an example: http://sqlfiddle.com/#!2/7de26/1

MySQL get the column name from column value

Is there is a way MySQL to get the column names given the value of the column?
For example I have STATES table and CITY table which have 'NAME' as column name.
Say I have 'New York' in both STATES table and CITY table. I want a query which will return the column names of SATES and CITY give 'New York'.
My Expected Output could be like
value column Table
----- ------ -----
New York NAME STATES
New York NAME CITY
Above is just an example. In most of the cases i only know the column values and i dont know the Column names or the corresponding table Names. Generally I need not know on the diffrent random schema's. I am building a question answersing system that will work with multiple databases.
When querying, to help prevent ambiguity, always qualify the fields with alias.column such as
select states.name,
city.name as CityName
from
city
join states
on city.stateabbrev = states.stateabbrev
where
city.name = 'New York'
you must be specified the column name to select data,
get column name,you can run SQL query:
desc tablename
or using like ResultSetMetaData(in java)

Normalizing MySQL table with records of another table

i have 2 tables. The city tables is not normalized because the country information is in plain text. I have added the id_country to the 'city' table (that column is empty).
I need to check for matches between city>country and country>country and then update the city records that matched with the id_country from the country table. At the end i will be able to delete the 'country' column from the city table.
City table
id_city (1, 2, 3...)
city (Washington, Guayaquil, Bonn...)
country (Germany, Ecuador, USA...)
id_country (currently empty)
Country table
id_country (1, 2, 3...)
code (GE, EC, US...)
country (Germany, Ecuador, USA...)
I have no idea on where to start and if it can be done with a SQL query. My original idea was to search for matches in a php loop but that seems to be a really harder implementation.
You can do this with a JOIN on an UPDATE statement.
UPDATE city c1 INNER JOIN country c2 ON c1.country=c2.country
SET c1.id_country=c2.id_country;
Using an INNER JOIN will make sure that updates only occur for cities that have a matching country value.
Once you've run it, you'll be able to select all those cities that still have a null id_country just in case some of them didn't match. Conversely, once you've determined that all your cities have an id_country, you can delete that column from the city table.
The city tables is not normalized because the country information is
in plain text.
Nonsense. Normalization doesn't mean "replace plain text with id numbers". Find whoever taught you that and poke him in the eye with a sharp stick.
Your real problem is that "city" plus "country" isn't sufficient to identify cities, at least in the USA. I think there are at least a dozen different cities named "Washington" in the USA.
Instead of replacing the country name with an id number, you'd be far better off replacing it with the two-letter country code. The codes are human-readable; the id numbers will require an additional JOIN in every query that uses your table of cities.
Something like this should work:
UPDATE city set id_country = (SELECT country.id_country from country WHERE country.country = city.country)

Searching in a database with over 3 mil. entries

I'm using Geonames database for a hotel booking website. The database has two tables, one for countries, and one for cities with over 3 mil. entries. If I try to get all the cities for a specific country the query is too slow. I think is because I don't have any index defined.
The countries table has the following fields:
iso_alpha2 (country code)
name
continent
population
The cities table has the following fields:
name
asciiname
alternate_names
country
The "country" field from the cities table relates to "iso_alpha2" field in the countries table.
How can I speed up the query?
P.S. I'm using MySQL.
You need to add an index on the field that you use in the WHERE clause (in your case it seems to be the country field).
Edit: one more thing - if you have multiple conditions in the WHERE clause you need to add an index that contains all the fields used in that clause (having separate indexes on the fields won't work). However in your case I believe that the index on the country field should do.
For this query, you'd only need the cities table:
select name from cities where country = 'US'
This query would benefit from an index on country.