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.
Related
I am fairly new to ms access (working with access 2013) and unfortunately am stuck with a problem.
I am currently working on an update query with 2 tables. In table 1 I would like to update all fields of a column with a "1" based on multiple criteria. Three different criteria exist in both tables. I only want to update the column if 2 of the criteria are exactly the same in both tables and one criteria is larger in table 2 than in table 1. However, unfortunately even if all criteria match, that does not mean that the certain case is unique. However, I just want to Update a "1" only once per unique row of table 2.
So basically, I have to questions:
Is the current code correct concerning the match I want to make?
Is there any way to tell access to only update once per unique row in table 2?
Thanks a lot for your help!
This is my current code:
UPDATE Table2 LEFT JOIN [Table1] ON (Table2.Criteria1 = [Table1].Criteria1) AND (Table2.[Criteria2] = [Table1].[Criteria2]) SET [Table1].Column = 1
WHERE (((Table2.[Criteria1])=[Table1].[Criteria1]) AND ((Table2.Criteria2)=[Table1].[Criteria2]) AND ((Table2.Criteria3)>=[Table1].[Criteria3]));
A left join and a where on same table work as an inner join. Looking to your code seems you need a join between table1 and table2 for update table2. So the syntax and the condition should be:
UPDATE Table2
SET [Table1].Column = 1
FROM Table2
INNER JOIN [Table1] ON Table2.Criteria1 = [Table1].Criteria1
AND Table2.[Criteria2] = [Table1].[Criteria2]
AND Table2.Criteria3>=[Table1].[Criteria3]
But if you need an update only for a single rows the you could trying using the min(id) resulting from the matching row:
UPDATE Table1
SET [Table1].Column = 1
WHERE Table1.ID = (
SELECT MIN(ID)
FROM Table2
INNER JOIN [Table1] ON Table2.Criteria1 = [Table1].Criteria1
AND Table2.[Criteria2] = [Table1].[Criteria2]
AND Table2.Criteria3>=[Table1].[Criteria3]
)
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
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 run into a problem where I'd like to copy data between columns based on a condition from a related table. Looking at the top answer from eglasius on this similar problem similar problem I came up with this solution:
UPDATE table1 SET table1.column2 = table2.column1
FROM table1 NATURAL JOIN table2
WHERE table2.column1 = "myCondition"
This query gave me a syntax error beginning at FROM although replacing the UPDATE clause with a SELECT seemed to yield no problems.
It seems that in the case of an UPDATE mySQL appears to dislike a FROM syntax. I had good success moving the join to the front of the query, following it with the JOIN and finally the WHERE condition, like this:
UPDATE table1 NATURAL JOIN table2
SET table1.column2 = table1.column1
WHERE table2.column1 = "myCondition"
I've got 3 MySQL MyISAM tables: table1, table2 and table3.
Each table has an ID column (ID, ID2, ID3 respectively), and different data columns.
For example table1 has [ID, Name, Birthday, Status, ...] columns,
table2 has [ID2, Country, Zip, ...],
table3 has [ID3, Source, Phone, ...]
you get the idea.
The ID, ID2, ID3 columns are common to all three tables... if there's an ID value in table1 it will also appear in table2 and table3. The number of rows in these tables is identical, about 10m rows in each table.
What I'd like to do is create a new table that contains (most of) the columns of all three tables and merge them into it.
The dates, for instance, must be converted because right now they're in VARCHAR YYYYMMDD format. Reading the MySQL manual I figured STR_TO_DATE() would do the job, but I don't know how to write the query itself in the first place so I have no idea how to integrate the date conversion.
So basically, after I create the new table (which I do know how to do), how can I merge the three tables into it, integrating into the query the date conversion?
Update:
The only thing that's unclear to me is how I can convert the dates within the query.
As far as I understand the query should be something like that:
INSERT INTO [new table]
SELECT table1.ID, table1.Name, table1.Birthday, table2.Country, table3.Phone
FROM table1
INNER JOIN table2 ON table1.ID = table2.ID2
INNER JOIN table3 ON table1.ID = table3.ID3;
...but how can I convert the dates within it? Or for that matter, apply any function to a field before it's inserted? For instance how can I convert the Birthday field before inserting it using STR_TO_DATE()? Where do I put it?
STR_TO_DATE(table1.Birthday, '%Y%m%d')
[Err I figured just replace "table1.Birthday" with "STR_TO_DATE(table1.Birthday, ...)"? Is that correct?]
Looks like you want an INSERT SELECT query along the lines of:
INSERT INTO [new table]
SELECT [values]
FROM table1
INNER JOIN table2 on table1.ID = table2.ID2
INNER JOIN table3 ON table1.ID = table3.ID3;
Where you fill in [new table] as the name of the new table and [values] as the values you want in the new table.
Here are the relevant parts of the manual for more details.
INSERT...SELECT syntax - for details of the INSERT SELECT statement
JOIN syntax - for details on JOINing tables in queries