Performing calculations when inserting in MySQL - 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.

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 can I copy rows from one to another table with a different colnm data

I had two tables Table 1 & Table 2 AS shown here
Table:1
ID
IMG_PATH
CAT_ID
166
hfhbf
1
164
jgj
2
162
ggd
1
160
mfnf
1
158
dbd
2
Table:2
ID
IMG_PARENT_ID
Here I want to print table 1's ID column data Example:166
Here (ID-1) Example:165
Here I want to print table 1's ID column data Example:164
Here (ID-1) Example:163
Here I want to print table 1's ID column data Example:162
Here (ID-1) Example:161
Here I want to print table 1's ID column data Example:160
Here (ID-1) Example:159
Here I want to print table 1's ID column data Example:158
Here (ID-1) Example:157
AS SHOWN IN TABLE 2 I NEED FOLLOWING VALUE...
and dont try this manually method:
INSERT INTO tabla2
SELECT * FROM tabla1
WHERE id = 1 //Here we write the condition
I want to fetch data because arround 10,000's row are inserted in this table
Lots of tries but didnt get it
based on what you provided info about your question, this is what I understand about this.
Assuming that table 1 is auto_increment with ID of 1-10,000.
Then you can use this to select the even IDs in table 1 and insert it to table 2
insert into table2 (ID) select ID from table1 group by ID having mod(ID, 2) = 0;
To select odd IDs from table 1 and insert it to table 2 you can use this
insert into table2 (IMG_PARENT_ID) select ID from table1 group by ID having mod(ID, 2) = 1;

Find single entries where there should be 2

I am looking to find all the single entries in a table where there should only be double entries.
Eg.
Unique_Key
ID
State_Sequence_ID
Localisation_Format_ID
File_Name
6644106
1315865
100
1
2064430-DNK.pac
6644107
1315865
190
2
2064430.chk [DNK]
I am looking to find all instances where the 2nd record does not exist.
The ID for each record will always be the same (although I do not know what that ID will be specifically) and the Localisation Format ID will always be 1 and 2. I am looking to find all entries where Localisation Format ID 2 does not exist.
SELECT *
WHERE ID has Localisation_Format_ID = 1
but does not have Localisation_Format_ID = 2
This is a simple not exists criteria:
select *
from t
where not exists (
select * from t t2 where t2.Id = t.Id and t2.Localisation_Format_ID = 2
);

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 find "holes" in auto_increment column?

when I DELETE, as example, the id 3, I have this:
id | name
1 |
2 |
4 |
5 |
...
now, I want to search for the missing id(s), because i want to fill the id again with:
INSERT INTO xx (id,...) VALUES (3,...)
is there a way to search for "holes" in the auto_increment index?
thanks!
You can find the top value of gaps like this:
select t1.id - 1 as missing_id
from mytable t1
left join mytable t2 on t2.id = t1.id - 1
where t2.id is null
The purpose of AUTO_INCREMENT is to generate simple unique and meaningless identifiers for your rows. As soon as you plan to re-use those IDs, they're no longer unique (not at least in time) so I have the impression that you are not using the right tool for the job. If you decide to get rid of AUTO_INCREMENT, you can do all your inserts with the same algorithm.
As about the SQL code, this query will match existing rows with the rows that has the next ID:
SELECT a.foo_id, b.foo_id
FROM foo a
LEFT JOIN foo b ON a.foo_id=b.foo_id-1
E.g.:
1 NULL
4 NULL
10 NULL
12 NULL
17 NULL
19 20
20 NULL
24 25
25 26
26 27
27 NULL
So it's easy to filter out rows and get the first gap:
SELECT MIN(a.foo_id)+1 AS next_id
FROM foo a
LEFT JOIN foo b ON a.foo_id=b.foo_id-1
WHERE b.foo_id IS NULL
Take this as a starting point because it still needs some tweaking:
You need to consider the case where the lowest available number is the lowest possible one.
You need to lock the table to handle concurrent inserts.
In my computer it's slow as hell with big tables.
I think the only way you can do this is with a loop:
Any other solutions wont show gaps bigger than 1:
insert into XX values (1)
insert into XX values (2)
insert into XX values (4)
insert into XX values (5)
insert into XX values (10)
declare #min int
declare #max int
select #min=MIN(ID) from xx
select #max=MAX(ID) from xx
while #min<#max begin
if not exists(select 1 from XX where id = #min+1) BEGIN
print 'GAP: '+ cast(#min +1 as varchar(10))
END
set #min=#min+1
end
result:
GAP: 3
GAP: 6
GAP: 7
GAP: 8
GAP: 9
First, I agree with the comments that you shouldn't try filling in holes. You won't be able to find all the holes with a single SQL statement. You'll have to loop through all possible numbers starting with 1 until you find a hole. You could write a sql function to do this for you that could then be used in a function. So if you wrote a function called find_first_hole you could then call it in an insert like:
INSERT INTO xx (id, ...) VALUES (find_first_hole(), ...)
This is a gaps&island problem, see my (and other) replies here and here. In most cases, gaps&islands problems are most elegantly solved using recursive CTE's, which are not available in mysql.