I have a table old_data and a table new_data. I want to write a select statement that give me
Rows in old_data stay there
New rows in new_data get added to old_data
unique key is id so rows with id in new_data should update existing ones in old_data
I need to write a select statement(I believe it should have joins) that would give me old_data updated with new data and new data added to it.
Example:
Table a:
id count
1 2
2 19
3 4
Table b:
id count
2 22
5 7
I need a SELECT statement that gives me
id count
1 2
2 22
3 4
5 7
You can use a join to select the data, you can use to as insert statement into a new table, but you cannot use a join to execute both insert and update statements.
Join is for doing query, not for doing insert or update.
In MySql the simplest way to do your job is:
REPLACE INTO old_data SELECT * FROM new_data;
Related
Table 1
Id
place
expiry_date
10
xyz
2022-09-12
Table 2 - expiry_date is the new column created in table 2. Need to fetch expiry date from table 1 where T1_id (in table 2) matches id (in table 1)
Oid
userid
expiry_date
T1_id
2
123
10
How to fetch expiry date (table 1 and fill the new column in table 2) only if the T1_id and Id(table 1) matches
Trying
insert into (sql) statements
Joins used
Join table1.Id on table2.T1_id
Insert statements won't allow you to change values from an existing table, they only allow you to add brand new rows to a table. In your case you may want to use an UPDATE statement.
In order to get matches between the two tables, you can apply a JOIN operation within the UPDATE statement, using the condition you pointed in your post description.
UPDATE tab2
INNER JOIN tab1
ON tab1.Id = tab2.T1_id
SET tab2.expiry_date = tab1.expiry_date;
Check the demo here.
It is possible in mysql create a view that use the column of a table plus the result of another for explain data?
I explain what I mean with an example.
Table 1
Table 2
Table 3
I want to select the record of table 2 and use in addition of record of table 1 and insert the data of table 3, like this creating a view that update if i adding row to any table.
the number on the column is num from table3 from id1 table1 and id2 from table2
I have 2 tables. Both contain 1 column with same value. I need to select all rows of one table if they have same value in another table.
How can do it?
I think what you are looking to do is
SELECT *
FROM table_1 INNER JOIN table_2
WHERE column.table_1 = column.table_2
This will return all the values that are in both table 1 and table 2 where the value in the column in table 1 is also in table 2
I have two tables
**Table A**
ID Name
1 abc
2 bcd
4 lmh
**Table B**
ID Name
3 abc
5 bcd
I don't know in which table ID=4 resides so I want to update the value with ID=4
how can i update the row with this?
I use UNION for selecting the value from multiple colmns but union is not working on update query.
I am using mysql.
Please help.
I don't think it's the good solution but I just run the update query two times. if it'll find some data with the requested id in either of tables it'll update the data
I have a database table with about 1M records. I need to find all duplicate names in this table and make them unique.
Id Name
1 A
2 A
3 B
4 C
5 C
Should be changed to...
Id Name
1 A
2 A-1
3 B
4 C
5 C-1
Is there an effective way of doing this with a mysql query or procedure?
Thanks in advance!
I needed to do something similar to a table I was just working on. I needed a unique URL field but the previous keepers of the data did not keep these constraints. The key was to create a temp table.
I used this response here to help: MySQL Error 1093 - Can't specify target table for update in FROM clause
Take note that it doesn't perform well, but then again if you only need to run it once on a database to clean a table then it shouldn't be so bad.
UPDATE `new_article` `upd`
SET `upd`.`url` = CONCAT(`upd`.`url`, '-', `upd`.`id`)
WHERE `upd`.`url` IN(
SELECT `url` FROM (
SELECT `sel`.`url` FROM `new_article` `sel`
GROUP BY `sel`.`url` HAVING count(`sel`.`id`) > 1
) as `temp_table`
);