Updating database table based on condition set in another table? - mysql

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.

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

Merging 2 tables preserving the ID

I have a question about merging a table with another preserving an ID on a database (I'm using MySQL). I have 2 tables, the first has and Item ID and a category and subcategory assigned to that ID. The second has a Item ID with all its characteristics like name and other variables. How can I merge those two tables in a way that the ID corresponds to the correct item in the new table (that's the difficult part I think)? Is it possible?
Thank you for all the help!
It's a very basic operation called Inner Join:
Select *
from table1
inner join table2
on table1.itemid = table2.itemid;
EDIT: As OP wants to create a new table with the fields return by above query and insert data into newly created table; following are the query to insert data once its created:
Insert into tablename
Select *
from table1
natural join table2;
Note: Make sure that the order and datatypes of columns in new table and in the result of above select query must be same.
I'm assuming you want to create table from the combined results. See this page for details.
Basically you write and test the SQL query then CREATE TABLE table_name AS sql_query
create table new_item_table
as
select
a.item_id,
a.category,
a.subcategory,
b.item_name,
b.item_char_1,
b.item_char_2
from
item_category a inner join item_char b on a.item_id = b.item_id;
This will Do:
select a.*,b.ItemName,b.ItemChar1,b.ItemChar2 from FirstTable a join select * from SecondTable b on a.ItemId=B.ItemId;
Use left join if some of the records are not there in the second table

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

MySQL Update statement after adding extra condition at WHERE

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'

MYSQL request from two tables, in order to display a table

I have got a MySQL database, managed with phpMyAdmin.
I am not very clever at MySQL requests.
In table1 are (among others) the 2 following columns :
- id_product
- active
In table2 are (among others) the 2 following columns :
- id_product
- description
I would like to write a request that display a table as follows :
having, at least, the id_product column and the description column
and having only product for which the active field is equal to 1 (the active field can only have a value of 0 or 1)
Thank you in advance for any help in this matter.
Patrick
You want to make an Inner Join between the tables. This matches rows from one table to rows in another, excluding rows that don't have a match. Then you want to add a restriction to only return those with active = 1.
This looks like:
Select t2.id_product, t2.description from table2 t2
inner join table1 t1
on t1.id_product = t2.id_product
where t1.active = 1;