MySQL get the column name from column value - mysql

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)

Related

MYSql Code to Display data from two tables into Single one

I have two tables States and Cities.
In teststates table: there are data regarding states of different countries
The code column has id for states associated with it
In Cities table:
there is region column which has same id as of code column in teststates table.
Requirement:
I want the Code in mysql to fetch id from the teststates table column and replace it into region column in testcities table as I want only two columns which are city name and region in testcities table. Please help me!
select a.name,b.region from teststates a,testcities b where a.country = b.country
This will return City Name and Region Name columns joining on Country name from both tables.

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

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.

Update Based on a common value where there are multiple matches

I've got two tables (MySQL database), one called cities (that has columns 'state_id' and 'stateAB'--state_id is the row I would like to fill, stateAB is the 2-letter code of the state--I want this to serve as the key value).
I have another table called states (that has columns 'id' [this is the value that I want to go into the 'state_id' field of 'cities'], and a 'title' field [2-letter state codes] to be the common-key value).
I wanted to use a simple:
UPDATE cities SET state_id=(SELECT id FROM states WHERE states.title=cities.stateAB)
With the idea being that state_id will be set to the id that is returned where the 2-letter codes match.
The problem is that the following is returned:
#1242 - Subquery returns more than 1 row
I assume this is because there are more than one time per state that the codes match, for the simple reason that there are multiple cities per state (and they all have the same state/codes).
I'm not sure how to change this to make it work--I'm sure it's something obvious I'm just missing, but I don't know how to deal with the issue.
This is your query:
UPDATE cities
SET state_id = (SELECT id FROM states WHERE states.title = cities.stateAB);
You are getting the error because states has duplicates in the title column. You can find these by running:
select title, count(*) as numdups
from states
group by title
having count(*) > 1;
You may not care about the duplicates, happy to select just one id (consistently) when there is a match. If so, you can do:
UPDATE cities
SET state_id = (SELECT MIN(id) FROM states WHERE states.title = cities.stateAB);

Update table based on other 2 related tables

I have a strange problem. I got some data for cities, regions and countries in CSV format and imported them into MySQL tables.
I have 3 tables and their fields
1. City : id, name, country_code, region_number
2. Region : region_number, country_code, name
3. Country : country_code, name
Now things get a little complicated, as I added an auto-generated id column to the region table, so the region x for country y would be unique.
The thing is: Now i am trying to update city field region_number to hold this unique value (the new id column in region) so I can have relations city->region.
The relation region->country or country->region is OK.
Is it possible to write an update query that would update city region_code (or fill some new column, eg. region_id) with correct values?
If not an query, what could I use to get the correct values into the cities table?
I have arround 3 million records!
If I understant correctly, I think you are looking for something like this:
UPDATE
City inner join Region
on City.country_code = Region.country_code
and City.region_number = Region.region_number
SET
City.new_column = Region.id
However, since there's a relation already between City and Region, I am not sure this is the right thing to do, since it will make the table not normalized.
Now i am trying to update city field region_number to not hold this unique value
The only way you can do this is if the region_number uniquely identifies each region - and if that's already the case then you are wasting your time by creating redundant references. Although frankly, if these really are your table structures, there's no reason for using surrogate keys. And if there's no reason for using surrogate keys then the region and country table are redundant.

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)