How to update one table in MYSQL from another table? - mysql

I have two tables
Table tool
column names:
id toolnumber currentduedate
1 123 11/3/2015
2 456 11/3/2015
3 789 11/3/2015
Table event
column names:
id eventnumber newDuedate
7 123 11/3/2015
9 123 11/3/2015
10 456 11/3/2015
What i want is when i update the newDuedate in table event it should update the currentduedate in tool table.
I am using this query:
mysql_query
UPDATE tool INNER JOIN event SET tool.currentduedate = event.newDuedate WHERE tool.toolnumber = event.eventnumber ;
is working fine but if i have 2 field with the same eventnumber this query update only one. Any ideas?

Try this way
UPDATE tool
INNER JOIN event on tool.toolNumber = event.eventnumber
SET tool.currentduedate = event.newDuedate ;

Related

SQL update where select from the same table [duplicate]

I have a table. I want to update the 5th row with 10th row values from the same table. For example:
SlNo Name Quali Exp
1 x B.E 2
2 y BSC 3
3 Z B.A 1.5
4 A MSC 2
5 B MBA 5
Here i want to update second row with the value of 5th row.
Here is my current query:
UPDATE table
SET Name=(select Name from table where slNo='5'),
Quali=(select Quali from table where slNo='5'),
Exp=(select Exp from table where slNo='5')
where slNo='3';
this is working fine ... but if there are more than 20 columns it becomes laborious to write a query this way, because for each column I have to include another sub-query... is there any other way to write query to update the whole row with all values from the other row in the same table?
Use a self-join with the multiple table UPDATE syntax:
UPDATE `table` AS t1 JOIN `table` AS t2 ON t2.slNo = 5
SET t1.Name = t2.Name, t1.Quali = t2.Quali, t1.Exp = t2.Exp
WHERE t1.slNo = 3

How do I update multiple columns in a table with multiple conditions in MySQL?

Is it possible to run an update query on multiple columns with multiple conditions in MySQL?
id name value price instock pp_flag
1 xyz 23 27 1 9
2 abc 28 12 0 8
For example above is the structure of a table myTable, where I want to run a query like:
UPDATE TABLE myTable
set value = 25
where id = 1
and price = 12
where pp_flag = 8
Just wondering if I can do this in the same query in MYSQL.
Thanks
Use and in where clause:
UPDATE TABLE myTable
set value = 25
where id = 1
and price = 12 and pp_flag = 8
What I understand is that you would like to do this
UPDATE TABLE myTable set value = 25 where id = 1
and
UPDATE TABLE myTable set price = 12 where pp_flag = 8
in a single statement.
You cannot do this as these are two independent WHERE-conditions.
You can use multiple condition to update column of a table .
As per your requirement you can use below query:
UPDATE TABLE myTable set value = 25
where id = 1 or (price = 12 and pp_flag = 8);
Hope it will help you!
Yes, it is possible by using the inbuilt IF function in MySQL (https://dev.mysql.com/doc/refman/8.0/en/if.html).
Query for your example would be:
UPDATE myTable SET
value = if(id=1, 25, value),
price = if(pp_flag=8, 12, price)

How to delete a row where there are only one of the kind in MySql?

I have following data in MySQL table named info:
chapter | section
3 | 0
3 | 1
3 | 2
3 | 3
4 | 0
5 | 0
I would like to delete a row for chapter = n, but only when there is no section>0 for same chapter. So chapter 3 can't be deleted while chapter 4 and 5 can. I know the following doesn't work:
DELETE info WHERE chapter = 3 AND NOT EXISTS (SELECT * FROM info WHERE chapter = 3 AND section>0);
The same table is used twice in the statement. So what is the easiest way to achieve my goal?
You've got the idea right. Here is the syntax:
DELETE
FROM mytable
WHERE chapter NOT IN (
SELECT * FROM (
select tt.chapter
from mytable tt
where tt.section <> 0
group by tt.chapter
) tmp
)
The nested select is a workaround a bug in MySQL.
Demo.
You can run a sub query to return the rows that have sections of more then one and then delete the rows returned from the sub query.
DELETE FROM table1 WHERE table1.chapter Not IN (select chapter from
(SELECT table1.chapter FROM table1 WHERE Table1.section >=1 ) Results);
Example Fiddle based on your question
You could also supply the chapter as well in the sub query where clause if you only want to delete a specfic chapter. If it does not meet the where clause then no records will be deleted.
This should do it.
DELETE FROM Table t
WHERE NOT EXISTS(
SELECT 1
FROM Table t2
Where t2.chapter = t.chapter
And t2.section > 0
)
In my experience Exists generally performs better than In. If you are storing a large amount of records you should take this into consideration.

Update query on mutiple table in Sqlite or SQL

Hi I am new to SQL database.
I have two tables one is a "Master" and other is "Sub" like this
Master
uid(primary key) f_name l_name
1 fAaa lAaa
2 fBbb lBbb
second table
Sub
tid(primary key) uid(foreign key) time is_free
1 1 1:00AM 0
2 1 2:00AM 1
3 1 3:00AM 0
4 2 1:30PM 0
5 2 2:30PM 1
from both table we can say that user fAaa lAaa is free at 2:00AM and NOT free at 1:00AM and 3:00AM.
now I want to update like this, for user 1(fAaa lAaa), I want to delete time 2:00AM and want to insert new two time like 5:00AM and 6:00AM for user 1 than what should be my join query for update.
please help me!
Thanks
Like this?
DELETE FROM secondtable WHERE uid = 1 AND (time = "1:00AM" OR time = "2:00AM");
INSERT INTO secondtable (uid, time) VALUES (1, "5:00AM"), (1, "6:00AM");
Or
UPDATE secondtable SET time = "5:00AM" WHERE uid = 1 AND time = "1:00AM";
UPDATE secondtable SET time = "6:00AM" WHERE uid = 1 AND time = "2:00AM";
This is some pretty basic stuff, I recommend you do a search for "sql delete from", "sql insert into", "sql update" and look for beginner tutorials.

How to update the each record in sql server

I need to update each records in the table using the Update script
query 1:
Select acheivementsId
from Students
where student_id = 2
Result:: 61 records// Number of acheivementsid
query 2 :
Select acheivementsId
from Students
where student_id = 4
Result: 61 records// Number of acheivementsid
I need to update Student_id = 2 acheivementid's with student_id = 4 acheivementId's.
how to write the Update Statement for updating 61 records.
Thanks
Try this:
UPDATE Students
SET acheivementsId = (SELECT acheivementsId from Students where Student_id=4)
WHERE Student_id=2