MySQL Update statement after adding extra condition at WHERE - mysql

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'

Related

Update query based on multiple criteria only once per row

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]
)

Execute value in two table

I want to execute value from two tables. I have write query to execute value but i don't know is it wrong or true. I provide in following in query.
"SELECT a.id,a.name,b.address,b.pin FROM table1 a,table2 b WHERE a.id=b.id";
You want to JOIN the two tables. You are trying with an implicit JOIN notation that is deprecated and you should do it with an explicit JOIN like this:
SELECT a.id,a.name,b.address,b.pin
FROM table1 a JOIN table2 b ON a.id=b.id
This is untested since you didn't provide examples of your data but you can read it as:
Select id and name from table a, address and pin from table b joining
them on the id field of each that must match. Swow only those records
that match.
You can read more here

Updating database table based on condition set in another table?

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.

SQL Tables Joining issues

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

update a table using inner join syntax in mysql

i am curious about how updating a table with inner join works. if i run the following statement:
update tbl1 a
inner join tbl2 b using (id)
set a.val = b.val;
what happens to the records in tbl1 that do not have a match in tbl2? will they simply not be updated and left as is in tbl1? will they be deleted?
i realize i can run this and get the answer but i'm also interested in the mechanics of how this works behind the scenes and was hoping somebody could elucidate this for me.
The update statement is operating on tbl1.
The join is providing a filter that specifies which rows to update in tbl1.
As an added bonus, the join is also providing a value for the column.
The update statement does not and cannot delete rows from a table.
Q: What happens to the records in tbl1 that do not have a match in tbl2?
A: They will not be updated since they have no match from tbl2. From the definition, the INNER JOIN keyword return rows when there is at least one match in both tables. The only keyword that could delete record from your table is the DELETE DML.