I have a database with two separate tables. One table (T1) has 400+ values in its only column, while the other (T2) has 14,000+ rows and multiple columns.
What I need to do is to compare the column in T1 to one column in T2. For every matching value, I need to update a different value in the same row in T2.
I know this is pretty easy and straight-forward, but I'm new to MySQL and trying to get this down before I go back to other things. Thanks a ton in advance!
EDIT: Here's what I've been trying to no avail..
UPDATE `apollo`.`Source`, `apollo`.`Bottom`
SET `Source`.`CaptureInterval` = '12'
WHERE `Bottom`.`URL` LIKE `Source`.`SourceID`
EDIT 2:
A little clarification:
apollo.Bottom and apollo.Source are the two tables.
apollo.Bottom is the table with one column and 400 records in that column.
I want to compare Bottom.URL to Source.SourceID. If they match, I want to update Source.CaptureInterval to 12.
You can use the following query to update. But the performance will be much better if you index URL and SourceID columns in both tables as they are being used in the WHERE clause.
UPDATE `apollo`.`Source`, `apollo`.`Bottom`
SET `Source`.`CaptureInterval` = '12'
WHERE `Bottom`.`URL` = `Source`.`SourceID`
You can join the two tables together and do a multiple table update.
Start with something like this:
UPDATE `apollo`.`Source`
INNER JOIN `apollo`.`Bottom` ON `apollo`.`Bottom`.`URL` = `apollo`.`Source`.`SourceID`
SET `apollo`.`Source`.`CaptureInterval` = '12';
Related
So, I have a members table that we'll call table_members. Among others, it has a column called email.
There's also a table called table_oldnewemail. It has two columns: old_email and new_email.
I'd like to:
1. Find the row in table_members where table_members.email equals table_oldnewemail.old_email
2. Then replace table_members.email with the corresponding table_oldnewemail.new_email
3. Repeat for all table_oldnewemail values
I feel I have all the necessary parts, but is this even possible with a MySQL query? What would be an otherwise smart way to automate such a process?
Did you try something like this:
UPDATE table_members t INNER JOIN
table_oldnewemail tno
ON t.email = tno.old_email
SET t.email = tno.new_email;
There are many varied posts about this matter, but I am unable to find the answer I need. I am hoping this question is unique.
I am trying to append all the data from one table to another, without creating new records. The data in the second table is really a subset of data for a portion of the existing records in the first table.
For example:
I have the table "SPK". And I want to write all of the data from SPK into the table "RCT". The common field between each record I want to match is the RegID, which is unique in both tables (i.e. there is only one SPK record per RCT record).
If I understand correctly, you mean append the columns in one table (call it SECOND) to the other (call it FIRST).
In that case, does this work ?
UPDATE
regcontactsTest
JOIN
speakersTest
ON speakersTest.RegistrationID = regcontactsTest.RegistrationID
SET regcontactsTest.presentationtitle = speakersTest.presentationtitle
EDIT: Updated the query based on Mariadb syntax
You need to use JOIN. For general Update join :
update tab1 a
join tab2 b ON a.join_colA = b.join_colB
SET a.columnToUpdate = [something]
Or in other words:
update
tab1 a
join tab2 b on ..
set a.field=...;
What do I have now is a two tables in MySQL db, one thing happened during converting tables (forum convert), and now I've an issue with encoding. I want to fix that by joining one table to another, according to ids, and ignore first table text column while replacing it by text column from other table.
Both tables have "topic_id" and "threadid" which uses same numbers to identify thread name.
They are also have "title" and "topic_title". There is some amount of other columns, ask if you will need and I post the other ones.
So, is it possible to check while "topic_id == threadid", and replace "topic_title" with "title" using MySQL query or not?
UPDATE phpbbf_topics t1
JOIN vb_thread t2 ON t1.topic_id = t2.threadId
SET t1.topic_title = t2.title
Something like this should do it.
yes it is possible try this query
Update tbl1 A SET A.topic_title = B.title
LEFT JOIN tbl2 B ON A.topic_id = B.threadid
Well I m pretty stuck in this problem, I have two tables with identical structure, I want to update first table with values of 2nd table. I have following query but mysql is throwing the error.
UPDATE property p
SET ROW = (SELECT * FROM temp_property t WHERE p.id= t.id)
Can anybody shed some light on this?
I'm pretty sure you can't update an entire row all at once. You need to specify the column names.
UPDATE property p, temp_property t
SET p.col1 = t.col1, etc
WHERE p.id=tp.id
(Fixed query for MySQL.)
I have a dynamic amount of tables with the same column structure. I wish to update certain rows in all of them with a single query. Any multi-table UPDATE examples I've found have to do with doing different columns in different tables, and updating one table based on the value in another table. My issue is that I have the same column in all tables, and it is a dynamic number of tables.
As an example of this, I would have table_a, table_b, and table_c. All three tables would share the same structure: id and status.
I want to update the status column in all three at once to a known value.
This is the queries that I would like to essentially combine in this example.
UPDATE table_a SET status = 'closed' WHERE id = 5
UPDATE table_b SET status = 'closed' WHERE id = 5
UPDATE table_c SET status = 'closed' WHERE id = 5
Since the amount of tables is dynamic, I feel this would be cleanest with a single query. I have an array of the table names in PHP (loaded dynamically for this system) and can iterate them to form joins or concatenate a string if needed.
Here is an example of what I thought would work.
UPDATE table_a, table_b, table_c
SET status = 'closed'
WHERE id = 5
The problem with this query is that I get an error about ambiguity for the column names.
Do I need to make it like something this?
UPDATE table_a, table_b, table_c
SET table_a.status = 'closed', table_b.status = 'closed', table_c.status = 'closed
WHERE table_a.id = 5 OR table_b.id = 5 OR table_c.id = 5
I'm assuming this would work and might not be too bad, as the expected amount of tables really ranges from 1-10 maybe. However, I was really hoping for a more efficient way. Firing off individual queries might not be so bad with that few of queries, but in a big system every bit of optimization counts!
Thank you for any help.
Please note that the structure of having multiple tables with the same scheme is by design, as it is a tracker system that separates multiple locations. The example only reflects the basic structure of the idea I need to implement; the real thing is much more complicated. Also, stored procedures will not be an option in this project.
Yes, you'll need to qualify each table / column that you want to update if they use the same logical names.
The query sees you're looking for a logical name of 'status', but you've defined 'status' to be the logical name for 3 different physical locations in the database. Which one did you mean? There are many things that can be done when multiple tables are involved. So for a database to assume you want to update all of them would be very bad.