SQL update where select from the same table [duplicate] - mysql

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

Related

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)

SQL query: Show all from A that have B as C

Id Parinte Angajator
1 Parinte1 Firma1
2 Parinte2 Firma2
3 Parinte3 Firma3
Id Copil Data_Nastere Id_Parinte Data_creare
1 Copil1 10.01.2013 1
2 Copil2 11.11.2012 1
3 Copil3 10.10.2013 2
4 Copil4 12.11.2013 2
I have these 2 tables (1st let's say table1 and 2nd table2)
I need to do the following operations on these 2 tables.
I need them in a query so I can copy paste it for project!
Show all from "Parinti" that have "Angajator" as "Firma1"
Show all from "Copil" that have "Parinte1"
Update the field "Data_creare" from table2 with current date for "Copil" that have "Parent1"
Delete all from "Copil" that have "Data_Naster" = 10.01.2013
last I need to sort the values from table2 column "Copil" ascending ascending depending of field "Data_nastere"
At least you have to type it yourself!

How to update one table in MYSQL from another table?

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 ;

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.

Performing calculations when inserting in MySQL

The scenario im facing is given below:
Table 1:
ID Name Age1
1 X 22
2 Y 23
Table 2:
ID Name Age2
1 XX 45
2 YY 55
I need to add a new column in Table 1 which is equal to
(TABLE1.AGE1/TABLE2.AGE2) * 100
Can I perform this using MySQL?
Thank you in advance for your help
First Introduce the column, then add the data. Something like
ALTER TABLE Table1 ADD Ratio double NULL AFTER Age1;
UPDATE Table1, Table2
SET Table1.Ratio = (Table1.Age1/Table2.Age2) * 100
WHERE Table1.ID = Table2.ID
You can't do it with a single query.