MySQL - update whole column with records from parent table? - mysql

I have two tables t1, t2 that I have created and loaded data from a CSV into these.
I had to then create a new PK column as the existing columns (t1.old_id, t2.old_id) are strings that would naturally be a PK are not absolutely fixed (this seems to be advised against?)
so I created a id PK INT AUTO_INCREMENT in each table
as one record in t1 is linked to many in t2 and I want to maintain referential integrity between these two tables.
I believe what i need to do is create an id INT NOT NULL in t2 as an FK
This t2.id is blank at the moment (as it is dependent ont1.id`)
Am I right in thinking I need an UPDATE query with a JOIN of some description to make this work?
The following produces the data exactly that I want to update into my t2.id column - but I don't know how to do the update
select t1.id
from t1
inner join t2
on t1.old_id = t2.old_id

You can use a join in your UPDATE statement like this:
UPDATE t2
JOIN t1 ON t1.old_id = t2.old_id
SET t2.id = t1.id

You can use a correlated UPDATE query like this
UPDATE t2
SET id = (SELECT MAX(t1.id) FROM t1 WHERE t1.old_id = t2.old_id);
*Assuming you have a single t1.id for each t1.old_id
On a Separate Note, You should name t2.id like t2.t1ID so as to remove ambiguity if and when you have a identity column in t2 as well named id

Related

How to update a table from another one depending on certain conditions and getting data from a third one if the conditions are not met?

I need to update a table with values coming from another table while statisfying a few conditions in the meantime.
To be more specific, the 'source' field of table1 needs to get updated by the field 'value' from table2.
These 2 tables share a common 'id' field that can be used for a join. If the 'id' of table1 doesn't have any correspondence in table2, then it should take the value of the 'source2' field from table3. If the 'source2' value is 'NULL', then it should be given a default value that we will name 'Default' in this example.
As an example, we have the table1 below, which is the one we want to update:
Then we have the table2 below, which is the source table that will be used to update table1:
Finally we have table3, which will be used if the information in table2 is unavailable:
Based on this example, I would like to write a query that would update table1 with the values below:
I have written the following query using MariaDB but obviously it is not correct:
UPDATE table1 T1
LEFT JOIN table2 T2 ON T1.id = T2.id
LEFT JOIN table3 a ON T1.id = T3.id
SET T1.source = if(T2.value is NULL, if(T3.source is NULL, 'Default', T3.source), T2.value)
WHERE T1.id = T2.id
Which parts should be amended to make it work?
You seem quite close. The left join logic is fine, we just need to adjust the conditional set and the where clause:
update table1 t1
left join table2 t2 on t2.id = t1.id
left join table3 t3 on t3.id = t1.id
set t1.source = coalesce(t2.value, t3.source, 'Default')
where t2.id is not null or t3.id is not null
coalesce() returns its first non-null argument.
The where clause ensures that we don't update rows that match in neither tables. You can remove it if you want to update all rows (those that do not match will get 'Default' assigned).

Append data from first MySQL table to second by ID

I have two MySql Databases.
One has:
___id___|___name___|__date____|
Second database has
___id___|___tag___|
One common thing in this tables - id. It's same in both tables. How can i append column "tag" to first mysql database?
If you just want a view of the columns in the first table along with a possible matching tag, then use a query:
SELECT
t1.id, t1.name, t1.date, t2.tag
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id;
If you instead want to actually add a new tag column to the first table, then add that column and then do an update:
ALTER TABLE table1 ADD COLUMN tag VARCHAR(55);
UPDATE table1 t1
INNER JOIN table2 t2
ON t1.id = t2.id
SET t1.tag = t2.tag;

MySQL Import only one column to existing table

I have the following problem.
I have a table which contains the following fields (example):
id, id2, id3, id4
Due to a mistake, I have accidently deleted all values of id3. (They are NULL now).
I have a second file (backup) which is a bit older, so it doesn't have the same count as the damaged table. The id3 is present there.
How do I "join" these tables, to restore at least the bigger part? Insert id3 ONLY to the damaged table from the non-damaged table?
I tried this:
UPDATE table1 SET `id3` = SELECT `id3` FROM table2
In my case, only phpMyAdmin or SQL Syntax solution (no console) would work...
Please help!
If we assume that one, two, or three of the ids define each row, then you can use join:
update table1 t1 join
table2 t2
on t1.id = t2.id
set t1.id3 = t2.id3;
This assumes that id is unique in the two tables. You might want to use a more complex expression:
update table1 t1 join
table2 t2
on t1.id = t2.id and t1.id2 = t2.id2 and t1.id4 = t2.id4
set t1.id3 = t2.id3;
Assuming id is an unique attribute.
You can simply run this query:
UPDATE table1 SET id3 = (SELECT id3 FROM table2 WHERE table2.id = table1.id)

extracting extra records from mysql tables when there are no primary keys

I am trying to find all the records that are in t1 but not in t2. I know there are more records in t1 than in t2 because when I run
select count(*)
from t1;
select count(*)
from t2;
I get 21,500 records and 21,000 records respectively. But the problem is these tables are not normalized, there are no primary keys, therefore I cannot do something like this:
SELECT id FROM t1
where t1.id
not in (
SELECT t2.id
FROM t2
where t2.id is not null);
or this
SELECT t1.id, t2.id
FROM t1
LEFT JOIN t2
ON t1.id = t2.id
where t2.id is null
as both return null, as the id numbers match perfectly, there seems to be the same exact amount of ids. There must be another field which is not matching.
UPDATE
I ended up doing this:
select id, count(id)
from t1
group by id;
select id, count(id)
from t2
group by id
it gave the same amount of claim numbers and the count of times it shows up. I copied and pasted it into excel and just subtracted one count from the other and did a conditional formatting to only show the ones that are not zero and this gave me all the ids that showed up in one table more than the other. (Sloppy solution, but it was able to resolve the issue).
You have two problems. A bad database design and somehow bogus data is being inserted into your tables.
I don't know if this will work without indexes.
A left outer join should get you started (look up the syntax).
You should end up with something like:
t1.id t2.id
1 1
2 2
3 null
4 4
Try to fix the tables by adding a primary key on 'id' to both tables using MySQL:
ALTER TABLE t1 ADD PRIMARY KEY (id)
ALTER TABLE t2 ADD PRIMARY KEY (id)

Getting table data excluding constraints

Table1 - has constraints with Table2 & Table3
Table2
Table3
Any data which is present in table1 with constraints with table 2 & 3 is valid data.
There are some bogus data somehow entered in table1 by manually turning off the constraint.
I want to collect those data which is present only in table1 without any constraints.
Is there an easy way to get table1 data in mysql which don't have constraint data attached to it?
Thanks.
If I understand your question, you are looking for missing or bogus records in the parent table. I'm going to imagine that the constraint field in table2 and table3 is id and the fk fields in table1 are table2_id and table3_id. If this is the case, you'd query for missing joins:
SELECT t1.id, t1.table2_id, t1.table3_id FROM table1 t1
LEFT JOIN table2 t2 ON t2.id = t1.table2_id
LEFT JOIN table3 t3 ON t3.id = t1.table3_id
WHERE t1.table2_id IS NULL
OR t1.table3_id IS NULL;