Filling values of one table by querying another table - mysql

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.

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.

how to integrate all columns from one table into one column of another table using mysql

table 1: employee
name| mobile| location
alex| 123 | australia
john| 456 | paris
kohl| 678 | australia
table 2:employment
id|location |data
1 |australia|[{"name":"alex","mobile":"123"},{"name":"kohl","mobile":"678"}]
2 |paris |[{"name":"john","mobile":"456"}]
i have two tables named "employee" and "employment". How can i get all the column values of employee table into one column of employment table as shown in table 2. I am new to SQL querying. I honestly don't have any idea on how to proceed. Any pointers and suggestions are appreciated.
You can use the following solution using CONCAT and GROUP_CONCAT to get this result:
SELECT location, CONCAT("[", GROUP_CONCAT(CONCAT('{"name":"', name, '","mobile":"', mobile, '"}')), "]") AS data
FROM employee
GROUP BY location
demo: http://sqlfiddle.com/#!9/ab25ee/2/0
To JOIN the employee table with the employment you can use the following solution:
SELECT employment.id, employment.location, CONCAT("[", GROUP_CONCAT(CONCAT('{"name":"', name, '","mobile":"', mobile, '"}')), "]") AS data
FROM employment INNER JOIN employee ON employment.location = employee.location
GROUP BY id, location
demo: http://sqlfiddle.com/#!9/12f9c9/1/0

Show multiple unmatched records in SQL

I have two tables. These two tables may have ID's that do not match. However, also they may have names or addresses that do not match as well. I need to be able to filter out not only ID's but first_name, last_name and street_1 from my list. I can do a JOIN on match ID's but sometimes they match but the other columns may have records that do not match which I would need to show.
Find ID's that do not match. If they do match see if any of the other fields do not match.
Here are my expect results:
id first_name_2 last_name_2 street_1 street_2
3 Teresa White 834 Green Ridge Hill 43 Arapahoe Park
6 Rebecca George 39157 Nelson Hill 7467 Acker Center
7 Ann Hawkins 341 Tennessee Street 8 Bunting Street
8 Joyce Moreno 0277 Bunker Hill Drive 6 Nancy Center
9 Kimberly Alvarez 57332 Di Loreto Lane 0437 Waubesa Avenue
ID 3 & 6 is in the list because the Last Name does not match. ID 7 is last name and street_1. ID 8 & 9 ID's do not match.
Here is my sample data for reference: http://sqlfiddle.com#!9/928568/2
I would do the following: Left joining and treating nulls as blank strings. If you have a legitimate empty string, street_2 for example, it may return false positives:
SELECT *
FROM information I1
LEFT JOIN information_2 I2 ON I1.id = I2.id
WHERE ( I1.first_name_2 <> ifnull(I2.first_name_2, '')
OR I1.last_name_2 <> ifnull(I2.last_name_2, '')
OR I1.street_1 <> ifnull(I2.street_1, '')
OR I1.street_2 <> ifnull(I2.street_2, '')
);
Hi I went through the sample data reference and i feel your requirement is To find all the tuples whose exact copy is not there in there in the second table
You can use the following SQL code I tested this on your feedle and it is giving the expected result
SELECT
i.id, i.first_name_2, i.last_name_2, i.street_1, i.street_2
FROM
information i
LEFT JOIN
information_2 i2
ON
i.id=i2.id AND i.first_name_2=i2.first_name_2 AND i.last_name_2=i2.last_name_2
AND i.street_1=i2.street_1 AND i.street_2 = i2.street_2
where
i2.id is null
There is also a simple way to do this if your database supports MINUS set operator just write
SELECT * FROM information
MINUS
SELECT * FROM information_2
and you will get the same answer

mysql update rows of column in second table from column in first table where another column in first table matches column in second table

My title may be a little confusing, but this is basically what I want to do:
I have two tables:
Table 1 = Site
Columns: SiteID SiteName Address
1 Germany 123 A Street
2 Poland 234 B Street
3 France 354 F Street
4 England 643 C Street
5 Russia 968 G Street
Table 2 = Site_New
Columns: SiteID SiteName Address
1 Germany
2 France
3 Russia
I wan't to update the Address column in table 2 with the Address in table 1 where SiteName in table 2 = SiteName in table 1. As you can see there are sites in table 1 that are not in table 2, so I do not care about copying those addresses to table 2.
I was trying this code:
update Site_New set Address = (select Site.Address from Site where Site_New.SiteName=Site.SiteName)
but I was getting error code 1242: "Subquery returns more than 1 row."
Any idea on how this can be done?
You are better off using update/join syntax:
update Site_New sn join
Site s
on sn.SiteName = s.SiteName
set sn.Address = s.Address;
However, based on your sample data, your correlated subquery should not cause such an error.
Perhaps the join should be on SiteId rather than SiteName:
update Site_New sn join
Site s
on sn.SiteId = s.SiteId
set sn.Address = s.Address;
you need to do a select with your update like so
UPDATE site_new sn,
( SELECT
sn1.address as _address, sn1.sitename as _sitename
FROM site_new sn1
JOIN site s on s.sitename = sn1.sitename
) t
SET sn.address = t._address
WHERE sn.sitename = t._sitename

Return value from within a range in SQL Server 2008

I have two tables, one where it contains members & their cardnumbers, and outlet table which identifies the cardnumber ranges per outlet. I want to know which store the cardnumber the member has belongs to.
Members Table
MemberID Cardnumber FirstName LastName
1 123456123 John Doe
2 123456245 Sarah Smith
Outlets Table
OutletID OutletName StartCardNumber EndCardNumber
1 Balmain Store 123456100 123456200
2 Sydney Store 123456201 123456300
I can't think of a script which I can bring back the following information without having to
create a temp table first. Is there an easier way?
CardNumber FirstName LastName OutletName
123456123 John Doe Balmain Store
123456245 Sarah Smith Sydney Store
It's very simple. You join on a range using inequalities in addition to equalities.
SELECT
M.CardNumber,
M.FirstName,
M.LastName,
O.OutletName
FROM
dbo.Members M
INNER JOIN dbo.Outlets O
ON M.CardNumber >= O.StartCardNumber
AND M.CardNumber <= O.EndCardNumber
This is the same as M.CardNumber BETWEEN O.StartCardNumber AND O.EndCardNumber but I wanted to draw out the inclusive endpoints of your scheme using >= and <=. BETWEEN is not always suitable because very often the end value is exclusive requiring <, but not in this case.
Try this out right now online!