mysql updating one column to another column in a different table - mysql

I have a script that updates a table with infomation and I want to update another table with some of it.
In Table: products I have a column called last_updated and I want that info to be copied into Table: products_booze which also has a column called last_updated
Any help would be great!

following SQL will do the trick
update products_booze, products
SET products_booze.last_updated = products.last_updated
WHERE products_booze.prod_id = products.prod_id

Related

MS-Access 2016 - update a record based on a record from another table

I'm new to MS access. I have a few tables. Table 'Learners' contains columns such as 'LearnerID' (unique), firstname, surname, status, etc. There's another table (LearnerUpdate) that I created by importing a spreadsheet. The LearnerUpdate also contains a learnerID and their names. I need to match the learner ID and update teh LearnerUpdate table to include the 'status' taken from the 'Learners' table. How do I do it?
I'm not even sure what to google for. The Access terminology is alien to me.
Thank you
Assuming the column you want to update in the LearnerUpdate table is named 'Status', then try to execute this SQL in the Queries table:
UPDATE LearnerUpdate
INNER JOIN Learners
ON LearnerUpdate.LearnerID = Learners.LearnerID
SET LearnerUpdate.Status = Learners.Status

How to update a sql table only if other condition exist in another table

I have 2 sql separated tables in my database:
ds_users, containing: group_id
and
ds_users_data_members, containing: data_gender
I would like to set / Update the group_id to 6 for all data_gender equal to 2.
All this morning i tried to solve this issue , without success.
Please help. Thank you very much
I am assuming there must be a relation between those two tables. Without any relationship you cannot update record in one table by checking condition in another table.
let's say ds_users table has column user_id which is also exist in ds_users_data_members table.
so, you can write following query to update all records in ds_users for data_gender=2 in ds_users_data_members table
SQL SERVER EXAMPLE
UPDATE T
SET group_id=6
FROM ds_users T INNER JOIN ds_users_data_members T1 ON T.user_id=T1.user_id
WHERE T1.data_gender=2
MySQL EXAMPLE
UPDATE ds_users T INNER JOIN ds_users_data_members T1 ON T.`user_id`=T1.`user_id`
SET T.`group_id`=6
WHERE T1.`data_gender`=2;
You can replace the column name of user_id what you have given in your table.

How to fetch data using One to Many relationship using MySql and joins in MySql

I am creating a demo application. I am stuck in a scenario, I am not getting the exact way and query to fetch data from sql database in the following scenario:
I have a table named RegistrationTable, this table has a column RegistrationId as its primary key. There is another table named ApplicationDetails, this table has a column ApplicationId as its primary key.
I have referenced ApplicationId as Foreign key to RegistrationId column.
My requirement is, single user can apply to multiple jobs. job details will be present in ApplicationDetails table.
How can I check to how may jobs the user has applied based on his email id stored in registration table.
I have a column Status in ApplicationDetails table, where as soon as user applied to a job I am updating the status column.
I am trying the following query but its not working:
SELECT Status FROM ApplicationDetails
INNER JOIN RegistrationTable ON ApplicationTable.ApplicationId = RegistrationTable.RegistrationId
WHERE RegistrationTable.EmailId = "abc#xyz.com";
Can any one please suggest me how can I go about this. I am a beginner to SQL. Please do suggest a way to solve this. Thanks in advance.
You need to change the table name in your query to ApplicationDetails. This is what you mentioned in your post
SELECT Status FROM ApplicationDetails
JOIN RegistrationTable ON ApplicationDetails.ApplicationId = RegistrationTable.RegistrationId
WHERE RegistrationTable.EmailId = "abc#xyz.com";

Adding column from one table to the other table in mysql

How to add or insert specific column from one table to other ? I tried writing like this
ALTER TABLE info_apie_zaideja
ADD SELECT info_apie_match.Rank AFTER 'Nick'
FROM info_apie_match;
or this
UPDATE info_apie_zaideja
ADD COLUMN SELECT info_apie_match.Rank AFTER 'Nick'
FROM info_apie_match;
but that did not work. Oh, and the table where I want to insert column is view table if that helps somehow. All answers will be appreciated.
You need to do this in two steps. First alter the table to add the new column:
ALTER TABLE info_apie_zaideja
ADD COLUMN Rank INT AFTER Nick;
Then fill it in by copying from corresponding rows in the other table:
UPDATE info_apie_zaideja AS z
JOIN info_apie_match AS m ON z.id = m.zaideja_id
SET z.Rank = m.Rank
I had to guess at the column that relates the two tables. Correct the ON clause to match your actual table relations.
Also, consider whether you really need the column in both tables. With this redundancy, you'll need to make sure that whenever you update one table, the other one is updated as well. Instead, you could just use a JOIN whenever you need the value from the other table.

Mysql table sync or trigger?

What is the best way to do this with mysql :
I have two tables in the same database (a table : Gene, and a table Gcur).
In table Gene, I have a column last_updated. In table Gcur, I have a column last_modified.
I'd like to synchronize column last_modified with column last_updated.
For example, I made an update of column last_modified (from table Gcur), and automatically column last_updated (from table Gene) is updated. Two tables are linked by an ID key.
It should be possible with triggers ? An idea ?
Thanks !
Yes, it is possible with triggers, and fairly trivial. The result would look like
CREATE TRIGGER au_Gcur AFTER UPDATE ON Gcur
FOR EACH ROW
UPDATE Gene SET last_updated = NEW.last_modified WHERE id = NEW.id;