I have two tables, Table 1 and 2. I want to update information at Table 1 based on Table 2.
For example, correct AA in table 1 from 10 to 30.
What quires should I write?
Thanks,
you don't want to do a join from what I can tell, but instead you should do an update. It does get a bit more complicated when you're using data from another table instead of feeding raw data straight into the query.
UPDATE Table1 t1,
Table2 t2
SET t1.num = t2.num
WHERE t1.name == t2.name;
not the exact code of course because the question and tables are somewhat vague but I believe this is the right direction.
Try an update with a join.
UPDATE TABLE1 a
JOIN TABLE2 b
ON a.join_colA = b.join_colA
SET a.numberColumn = b.numberColumn
Here column join_colA is your first column numberColumn would be your other value column.
I would solve this problem in 3 steps
Step 1: Join the tables
Step 2: Update the null values from table 1
Step 3: Drop the unnecessary column
Select A.*, B.Column2 as column3 from A
left join B
on A.Column1=B.Column1
update table1
set column3= column2
alter table table1
drop column column2
Figure out the syntax errors that you may encounter
Related
How would I set a condition between 2 tables?
1 table has IDs whose values that need to be changed based on values set in another table.
The query I have so far...no idea if it works.
UPDATE table1 set value='2' INNER JOIN table2 WHERE CONVERT(value USING utf8) LIKE '%text%') so when value is 2 in the first table, that same item in table 2 will be assigned a specific category. The query should check that the ID in table 2 is the same ID that it found in table 1 that contained the value.
Your code looks like MySQL. If so, the correct syntax is more like this:
UPDATE table1 t1 INNER JOIN
table2 t2
ON t1.id = t2.id
set t1.value = '2'
WHERE CONVERT(t1.value USING utf8) LIKE '%text%');
Your question is a bit vague on the actual JOIN conditions, but that is the structure of the query.
I have this MySQL Update statement. It works fine.
UPDATE Table1
SET Table1_field1='field1_content', Table1_field2='field2_content'
where Table1_field3=2
All the fields above belong to the same table. I then added an extra condition AND Table2.fieldname='XXX' to the WHERE clause
UPDATE Table1
SET Table1_fieldname1='field1_content', Table1_fieldname2='field2_content'
where Table1_fieldname3=2 AND Table2.fieldname='XXX'
This time, the SQL statement fails. The error is "unknown column Table2.fieldname in where clause". However, Table2.fieldname does exist.
In order to be able to use fields from Table2 in your query you'll need use a JOIN between Table1 and Table2.
A JOIN effectively combines a row from each table into a single row for your query, based on a provided condition.
For example if both Table1 and Table2 have a column tableID, we can combine rows from each table where the tableIDs match.
The query would then look like below:
UPDATE Table1
JOIN Table2
ON Table1.tableID = Table2.tableID
SET Table1_fieldname1='field1_content', Table1_fieldname2='field2_content'
WHERE Table1_fieldname3=2 AND Table2.fieldname='XXX';
The JOIN keyword is equivalent to INNER JOIN. There are different types of JOINs available and I'd recommend reading up about them.
Here's a reference image to give you an idea of the different types:
you need to join table 1 and table2; then you can update
UPDATE Table1 AS b
INNER JOIN Table2 AS g ON b.id = g.id SET Table1_fieldname1='field1_content', Table1_fieldname2='field2_content'
where Table1_fieldname3=2 AND g.fieldname='XXX'
I've got 2 MySQL tables that I want to merge into one. I've been searching and I couldn't found the way of doing this specific merge:
I want to import a column from table B (key, column) to a new column of table A(key, column1,column2...). This is easy. But only the values of table B that its key is equal to a key of A, and I want it to be in that row.
For example: if I had this 2 tables:
TABLE1 TABLE 2
ID NAME ID TEAM
1 "name1" 1 "bt"
2 "bt2"
I want the result to be:
TABLE1
ID NAME TEAM
1 "name1" "bt"
Thanks!
You would use update with join:
update table1 t1 join
table2 t2
on t1.id = t2.id
set t1.team = t2.team;
However, there really is no reason to do this. You seem to be starting with a normalized data structure, and this is usually a good thing. You can just join in the value wherever you need it, perhaps by creating a view (if you really want).
I apologise for the rushed posting.
The following are images of what I have:
Table 1 called 'players'
Table 2 called 'Reports'
And this is the format that I want the table to be displayed in:
I have tried using simple 'join' and 'and' statments and using some other's that I found on here. Still no avail.
Any help would be great.
Im not sure i understand, but i think that's is what you need:
SELECT `t2`.`id`,`t1`.`name` `Reported Name`, `t12`.`name` `Reporting Name`
FROM `test`.`Reports` `t2`
JOIN `test`.`players` `t1`
on `t1`.`id`=`t2`.`reported_uid`
join `test`.`players` `t12`
on `t12`.`id`=`t2`.`reporting_uid`;
http://sqlfiddle.com/#!2/360d2/1/0
I agree with #Ohah that it's a little unclear, but I'll take a shot at it. If you're just trying to show all of Table 2 with one of the name fields from Table 1, you could do it this way:
SELECT t1.Example_Name, t2.*
FROM
Table_2 t2
JOIN Table_1 t1 ON t1.Name_1 = t2.User_Reported
Table 1 = t1 (t1 is an alias to Table 1 so you don't have to type the whole name out)
Table 2 = t2 (t2 is an alias to Table 2)
"Example_Name" is whichever field you want from the first table
The main question is how the two tables are related and what data you want to get from each. But I hope this helps.
Table1 contains all the fields from table2. I need to update table1 with the all the records from table2.
I found this:
UPDATE
table1
INNER JOIN
table2 ON (table2.id = table1.id)
SET
table1.field1 = table2.field1,
table1.field2 = table2.field2;
But I have too many fields and this would take forever to write. How can I update all the fields from table2 into table1? I canĀ“t seem to find the answer, please help.
I'm not terribly familiar with MySQL, but if you can get a list of column names, perhaps with:
SHOW COLUMNS FROM mytable FROM mydb
Then you can paste those into Excel and build your query, just paste your field names in column A , throw this in B1:
="table1."&A1&" = table2."&A1&","
And copy down.