MySQL update table with data from another table based on field - mysql

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.

Related

Filling values of one table by querying another table

I have two tables A & B. Both of them have common columns. A has most of those columns empty but B has most of them full.
I want to write a query which will select the columns in A which are not empty & use that to get the other empty columns info from table B & update them in A.
I'm guessing I will be needing select for update here but not sure. need help.
Table A
Name Address PhoneNumber
Nick 2nd St NY null
Dan null 123-456-7890
Table B
Name Address PhoneNumber Sex
Nick 2nd St NY 987-654-3210 M
Dan 5th St NY 123-456-7890 M
result should be that table A fills its empty columns by querying table B. The column SEX doesn't exist in A. A needs just the phone number & address since that's what is empty in A.
Result
Table A
Name Address PhoneNumber
Nick 2nd St NY 987-654-3210
Dan 5th St NY 123-456-7890
Something like this:
UPDATE
A INNER JOIN B ON A.column = B.column /*the columns that connect the tables*/
SET A.almost_empty_column = B.full_column
WHERE A.almost_empty_column IS NULL OR A.almost_empty_column = '';
But I'd suggest, that you have a look at a tutorial at least if not the manual and do not just copy&paste something some guy posts on the internet. You don't learn by not trying to understand what's going on.

Location search query issue

I have the following three tables Location, Country and CountryAlias. I want to find the locations country. Means I want to search location_name from location table against Country and CountryAlias and find the country code.
But while do like query. I am getting wrong output.
location
location_name
Bangalore, India
India
Chennai, India
Country
code name
IN India
IO British Indian Ocean Territory
CountryAlias
code alias
IN Bharth
IN Hindustan
Try Query
SELECT code from Country
LEFT JOIN CountryAlias ON Country.code = CountryAlias.code
where Country.`name` LIKE '%Bangalore, India%' or CountryAlias.alias LIKE '%Bangalore, India%'
O/P = NuLL
SELECT code from Country
LEFT JOIN CountryAlias ON Country.code = CountryAlias.code
where Country.`name` LIKE '%India%' or CountryAlias.alias LIKE '%India%'
O/P = IN and IO
Both wrong, Is there any perfect solution. I am using Innodb engine for tables.

MySQL: Get results given a condition

I have a table that looks like this:
target_id || country_code
5-----------||-------US----
5-----------||-------CA---
2----------||-------FR----
3----------||-------SP----
3----------||-------FR----
And another table that looks like this:
target_id || region_name
5-----------||---North America
2-----------||-----France------
3-----------||-----Some Europe
As you can see, table 2 contains locations and target_ids, while table 1 contains where these locations are targeted. In the case of North America, it is targeted to 5, which belongs to Canada and US. France, on the other hand has a target_id of 2, and Some Europe a target_id of 3, which contains France again and Spain.
What I would like to do via MySQL, is to get a table of target_id, country_code, country_name but only for countries. This means, only to the target_ids of table 1 that are in only one row (for example, we know that FR is a country because number 2 is only in FR, and we know that 3 represents a region because it has both Spain and France associated). Is this possible to do via MySQL or will I need two queries and PHP in the middle?
Thanks!
SELECT t1.target_id, t1.country_code, t2.region_name
FROM table1 t1
JOIN table t2
ON t1.target_id = t2.target_id
WHERE (SELECT COUNT(*) FROM table1 t3 WHERE t3.target_id = t1.target_id) = 1
table1 is the one with the country codes, table2 is the one with the the region names.

Eliminate duplicate rows using MySQL DISTINCT

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'

MySQL multi-table query

Greetings! I'm having difficulties creating a query that references multiple tables. I'm new to SQL and joins are stumping me. I've mucked about Google and after searching around I discovered Joining tables in SQL which got me closer to where I want to be! The query I've created isn't doing what I want. I have two tables:
disp_profile
disp_id* name address zip
0001 Profile1 SomeAddress1 11111
0002 Profile2 SomeAddress2 22222
0003 Profile3 SomeAddress3 33333
zipcode
zip_code* state city county
11111 CA City1 County1
22222 WA City2 County2
33333 NV City3 County3
What I am attempting to do is grab the City, State, County from zipcode when the zipcode.zip = disp_profile.zip (filtered by a variable State). Ideally returning something like:
dispId dispName dispAddress dispZip zipState zipCounty zipCity zipCode
001 Profile1 SomeAddress1 11111 CA County1 City1 11111
or
002 Profile2 SomeAddress2 22222 WA County2 City2 22222
SELECT
dp.disp_id AS dispId, dp.name AS dispName, dp.address1 AS dispAddress1, dp.zip AS dispZip, zc.state AS zipState, zc.county AS zipCounty, zc.city AS zipCity, zc.zip_code AS zipCode
FROM
disp_profile dp
INNER JOIN
zipcodes zc
ON
dp.zip = zc.zip_code
WHERE
dp.state = 'CA'
I'm aware this may not be the best way to go about this but I thought it better to store things separately vs redundant information for each item. The dataset in disp_profile is about 1000 records and zip_codes is about 30,000. Would this be done easier with a subquery? Thanks for your time!
No, you are doing it fine. This is what joins where made for, no need to use a subquery here. You could, but it would not perform better (the MySQL Query Optimizer might even translate it to a join internally).
If you worry about speed: Put the word "EXPLAIN" in front of your SELECT statement to see what MySQL is doing:
EXPLAIN SELECT
dp.disp_id AS dispId, dp.name AS dispName, dp.address1 AS dispAddress1, dp.zip AS dispZip, zc.state AS zipState, zc.county AS zipCounty, zc.city AS zipCity, zc.zip_code AS zipCode
FROM
disp_profile dp
INNER JOIN
zipcodes zc
ON
dp.zip = zc.zip_code
WHERE
dp.state = 'CA'
It will tell you how your statement is being executed. Paste the output here if you want us to help interpreting :)
That looks okay to me, at least at first glance. I prefer the old school way of:
select dp.disp_id AS dispId,
dp.name AS dispName,
dp.address1 AS dispAddress1,
zc.state AS zipState,
zc.county AS zipCounty,
zc.city AS zipCity,
zc.zip_code AS zipCode
from disp_profile dp,
zipcodes zc
where dp.state = 'CA'
and dp.zip = zc.zip_code
but that's just a matter of style (and using a DBMS with a very intelligent optimiser - whether MySQL is a match for my DBMS of choice, I couldn't comment (but I doubt it)).
The one change I have made (and which you probably should) is to only get one of the ZIP codes. It's redundant getting the field from both dp and zc since they're identical due to your join.