Updating column based on relation to another table - mysql

I have a table_1 that looks something like this
NameId SelectedName
Null A
Null C
Null F
NameId is a FOREIGN KEY that REFERENCES table_2 (Id)
And a table_2 that looks like this
Id Name
1 A
2 B
3 C
4 D
5 E
6 F
I want to populate NameId with all of the Id numbers associated with the same Name, so for example, the final result would be:
NameId SelectedName
1 A
3 C
6 F
So far, all I have is:
SELECT Id, `Name` FROM table_2 WHERE `Name` IN (SELECT `SelectedName` FROM table_1);
Which I then tried to follow up with:
UPDATE table_1 SET NameId = Id WHERE SelectedName = `Name`;
Which doesn't work. Not sure how to go about doing this or how to use previously selected columns and relations to get what I want done here...

UPDATE table1
INNER JOIN table2
ON table1.SelectedName= table2.Name
SET table1.NameId = table2.ID

UPDATE Table1 a INNER JOIN Table2 b ON a.SelectedName = b.Name SET a.NameId = b.ID

First Update the table_1.NameId values with table_2.Id
UPDATE table_1 INNER JOIN table_2 ON table_1.SelectedName= table_2.Name
SET table_1.NameId= table_2.Id
and retrieve the values from table_1
SELECT NameId, SelectedName
FROM table_1
WHERE SelectedName IN (SELECT Name FROM table_2);

This way will update relation table column when parent has update.
DROP TRIGGER `table_2_au`;
DELIMITER ;;
CREATE TRIGGER `table_2_au` AFTER UPDATE ON `table_2` FOR EACH ROW
UPDATE `table_1` T1
INNER JOIN `table_2` T2
ON T1.`SelectedName` = T2.`Name`
SET T1.`NameId` = T2.`Id`;;
DELIMITER ;

Related

How to differentiate name of columns in different tables?

I have two tables, their structure looks like this:
table 1: id, name, description
table 2: id, otherDescription
I want to set the value of "description" in table #1 to value "otherDescription" from table #2 respectively this id. I wrote the query:
UPDATE `table_1` SET description = (SELECT oldDescription FROM table_2 WHERE id = id))
How MySql will knows that in expression WHERE id = id first id taken from one table, and other id taken from second table? How to write this query correct? I think here must be used AS, but i dont know where
you can use table mane eg :
UPDATE table_1
INNER JOIN table_2 ON table_1.id = table_2.id
SET table_1.description = table_2.oldDescription
or table name alias
UPDATE table_1 a
INNER JOIN table_2 b ON a.id = b.id
SET a.description = b.oldDescription
You can join the two tables in the UPDATE, and give each an alias. The you identify the columns using alias.columnname:
UPDATE `table1` a
JOIN `table1` b ON a.`id` = b.`id`
SET a.description` = b.`otherDescription`

Update query in DB2 with values from second table where multiple entries exist for the same ID

I have two tables
Table 1
ID SELECTEDNAME
101 ______________
Table 2
ID NAME UPDATEDATE
101 JOHN 02-22-17
101 RITA 02-23-17
In Table 1 ID is PK. My requirement is to update the SELECTEDNAME column in Table 1 by extracting the name from Table 2
The name should be extracted from Table 2 such that the corresponding UPDATEDATE should be the latest.
For example, in Table2, I need a query to compare the UPDATEDATE in between the two entries for ID 1 and get the name as RITA as the UPDATEDATE is latest
There could be more than two entries for a given ID in the table.
try like this:
update t1
inner join (select t2.id,max(t2.updatedate) as updatedate
from t2
group by t2.id) t2max on t2max.id=t1.id
inner join t2 on t2.id=t2max.id
SET t1.selectedname=t2.name
WHERE t2.updatedate=t2max.updatedate
DB2 version :
UPDATE table1 f0
SET f0.SELECTEDNAME=
(
SELECT f1.name
FROM table2 f1 INNER JOIN
(
SELECT f2.id, max(f2.updatedate) maxdate
FROM table2 f2
GROUP BY f2.id
) f3 ON (f1.id, f1.updatedate)=(f3.id, f3.maxdate)
WHERE f1.id=f0.id
FETCH FIRST ROWS ONLY
)
WHERE EXISTS
(
SELECT * FROM table2 f1
WHERE f1.id=f0.id
)

MySQL update records of table with reference to columns of the same table

I am using MySQL 5.6.17.
I have a self-referencing table TableA with columns id (PK), title (varchar), type (varchar), parent_id (FK that refers to id of the same table).
The sample data is as below :
id title type parent_id
1 abc G NULL
2 def G NULL
3 xyz G NULL
4 pqr G NULL
5 abc T NULL
6 def T NULL
7 xyz T NULL
8 pqr T NULL
Now, I want each record having type='G' should become the child of the record with type='T' having the same title.
So the resultant table data should be :
id title type parent_id
1 abc G 5
2 def G 6
3 xyz G 7
4 pqr G 8
5 abc T NULL
6 def T NULL
7 xyz T NULL
8 pqr T NULL
I've tried query below :
UPDATE TableA
SET parent_id = (SELECT id FROM ( SELECT id FROM TableA WHERE TYPE='T' ) d)
WHERE TYPE='G';
But it returns
Error Code: 1242
Subquery returns more than 1 row
I also have tried :
UPDATE TableA t1
SET t1.parent_id = t2.newlocalid
INNER JOIN (
SELECT title, id AS newlocalid
FROM TableA t2
WHERE TYPE='T'
) t2 ON t1.title = t2.title
WHERE t1.type='G'
But it also returns the error in Syntax.
Can anyone help me to achieve it?
UPDATE TABLEA a
JOIN TABLEA b ON a.title = b.title and a.type='G'and b.type='T'
SET a.parent_id = b.id
This should work:
SELECT title, id AS newlocalid
INTO #t2
FROM TableA
WHERE TYPE='T'
UPDATE t1
SET t1.parent_id = t2.newlocalid
FROM TableA as t1
INNER JOIN #t2 as t2
ON t1.title = t2.title
WHERE t1.type='G'
Try:
UPDATE TableA a
SET parent_id = (SELECT id FROM TableA ref WHERE TYPE='T' AND ref.title=a.title)
WHERE TYPE='G';
to update the table column first you have to identify which value to set. you can not use multiple values to update the single column in same statement.
change your sql to something like this:
UPDATE TableA
SET parent_id = (SELECT id FROM TableA WHERE TYPE='T' limit 0,1)// i mean make sure that it is returning single record not multiple.or better add some more where condition to get a single and required record without using limit
WHERE TYPE='G';
or to some specific condition like this:
UPDATE TableA
SET parent_id = (SELECT id FROM TableA aa WHERE TYPE='T' and aa.type=TableA.type)
WHERE TYPE='G';

Update with count from same table

I have a table table_1 with a column parent_id with the value of the column id from other records from the same table.
I want to set another column out_degree with the number of records which have this table's id as their parent_id.
I tried this:
UPDATE table_1 p1
SET p1.out_degree = ( SELECT COUNT(*)
FROM table_1 p2,
table_1 p1
WHERE p2.parent_id = p1.id
GROUP_BY p1.id
)
But it didn't work. any idea?
Try this:
UPDATE table_1 T1
JOIN ( SELECT parent_id,Count(parent_id) as ParentCount
FROM table_1
GROUP BY parent_id
) T2 ON T1.parent_id=T2.parent_id
SET T1.out_degree=T2.parentCount
See the sample result in SQL Fiddle.

How do I merge two tables in SQL?

I've got two tables:
Table 1 = idhash - username - usermail
Table 2 = idhash - views - fistseen - lastseen
Now I want to merge these tables to a new table:
Table 3 = idhash - username - usermail - firstseen - lastseen
*notice that I want to drop the views column.
I've tried with solutions that I found on google, but so far they do not seem to work.
Not all the idhash columns from table 2 have a corresponding idhash in table 1. Stiil store those 'mismatched' rows with empty username and usermail
This query will give you the results:
SELECT A.*, B.firstseen, B.lastseen
FROM [Table 1] A
LEFT JOIN [Table 2] B
ON A.idhash = B.idhash
And to insert and update the results on your [Table 3]:
INSERT INTO [Table 3](idhash, username, usermail, firstseen, lastseen)
SELECT A.*, B.firstseen, B.lastseen
FROM [Table 1] A
LEFT JOIN [Table 2] B
ON A.idhash = B.idhash
LEFT JOIN [Table 3] C
ON A.idhash = C.idhash
WHERE C.idhash IS NULL
-- For SQL Server
UPDATE T3
SET firstseen = T1.firstseen,
lastseen = T1.lastseen
FROM [Table 3] T3
INNER JOIN (SELECT A.*, B.firstseen, B.lastseen
FROM [Table 1] A
LEFT JOIN [Table 2] B
ON A.idhash = B.idhash) T1
WHERE T3.firstseen != T1.firstseen OR T3.lastseen != T1.lastseen
Here's a solution for MySQL:
CREATE TABLE table3
// First get all the rows from table2, paired with matching rows from table1
(SELECT a.idhash, b.username, b.usermail, a.firstseen, a.lastseen
FROM table2 a
LEFT JOIN table1 b
ON b.idhash = a.idhash)
// Now get the remaining rows from table1 that don't have matches
UNION ALL
(SELECT null, a.username, a.usermail, null, null
FROM table1 a
LEFT JOIN table2 b
ON b.idhash = a.idhash
WHERE b.idhash IS NULL)
If you don't want the rows from table1 that don't have corresponding rows in table2, then delete the second query in the UNION clause.
You could just brute force it with your choice of programming language. Just build a new table, query both tables, join rows programmatically however they need to be joined, insert into new table.
insert into table3
select t1.idhash, t1.username, t1.usermail, t2.firstseen,t2.lastseen
from table1 t1 left join table2 t2 on t1.idhas=t2.idhas
This should be a good start. You need to tell us what to do with your mis-matched records before we can give anything more specific.
Select
table1.idhash
,username
,usermail
,firstseen
,lastseen
From table1
left join table2
on table1.idhash = table2.idhash
Create your new table with the field types in your create statement
http://www.w3schools.com/sql/sql_create_table.asp
Then do an insert Into Table3 <yourtable> select a.f1,b.f2,c.f3 from Table1 a, Table 2 b on a.id = b.id
This is pretty close to brute force.
select coalesce(t1.idhash, t2.idhash) as idhash
, username
, usermail
, firstseen
, lastseen
into table3
from table1 t1
cross join table2 t2
try this:
INSERT INTO table3 (idhash, username, usermail, firstseen, lastseen)
SELECT a.idhash, a.username, a.usermail,
b.firstseen, b.lastseen
FROM table1 a LEFT JOIN table2 b
ON a.idhash = b.idhash
I would do this:
CREATE TABLE table3
AS
SELECT idhash, username, usermail, firstseen, lastseen
FROM Table1
NATURAL FULL OUTER JOIN Table2