MySQL trigger update other rows on same table - mysql

I'm trying to update the same table after an insert's trigger.
I know that you can't update the same table on a trigger but I can't seem to find an alternative.
I have for example the following table :
id | team | time
1 A 1 568 725 200
2 A 1 568 725 200
3 B 1 568 824 950
If I update id 1 time from 1 568 725 200 to 1 568 455 800, id 2 time's should also be updated to 1 568 455 800
because they are in the same team (A).
Is there a solution to achieve this update inside the trigger's transaction?
I'm working on a bad designed database and I can't change much.

Related

Update the mysql field according to the sorting

a simple task, but I can't figure it out. There is a Table:
id
name
sum
place
1
Alex
210
0
2
Bob
250
0
3
Sam
190
0
4
Bill
290
0
5
Jack
210
0
I need to UPDATE the PLACE
according to the maximum SUM and then the id
Those are the request :
SELECT id, name, sum, place FROM tableORDER BYsumDESC,id ASC;
According to this request, update the semi PLACE from 1++
Those places 1, 2, 3 and further throughout the table
Like this:
id
name
sum
place
1
Alex
210
3
2
Bob
250
2
3
Sam
190
5
4
Bill
290
1
5
Jack
210
4
SET #n_place := 0;
UPDATE `table` SET `place` = #n_place := #n_place + 1 ORDER BY `sum` DESC, `id` ASC;

I'm trying to create dynamic SQL in MySQL

I use MySql and I have some problems with it ^^
Let's say I have two tables which will be "FirstTable" and "SecondTable"
First table (ID isn't the primary key)
ID IdCust Ref
1 300 123
1 300 124
2 302 345
And the second (ID isn't the primary key)
ID Ref Code Price
1 123 A 10
1 123 Y 15
2 124 A 14
3 345 C 18
[EDIT] The column "Stock" in the final result is equals to "ID" in the second table
The column "Stock", "Code" and "Price" can have x values, so I don't know it, in advance...
I make some research in stackoverflow, but I only find post where people use "count(case when ..."
Like this one : MySQL pivot row into dynamic number of columns
For my problem, I cannot use it, because i can't know in advance the value of reference
I'm trying to produce the following output:
The result I want
[EDIT] Result in TXT
ID IdCust Ref StockA Code1 Price1 StockB Code2 Price2
1 300 123 1 A 10 1 Y 15
1 300 124 2 A 14
2 300 345 3 C 18

How to populate data in one table from another table in MySQL

i have two tables in my toll database in MySQL, namely traffic_shift_wise and traffic_day_wise. And the columns of these tables are as follows:
Table traffic_shift_wise
toll_id date shift car_single car_return car_total
1 2016-09-01 1 25 10 35
1 2016-09-01 2 50 25 75
1 2016-09-02 1 100 50 150
1 2016-09-02 2 200 100 300
traffic_day_wise
toll_id date car_single car_return car_total
1 2016-09-01 75 35 110
1 2016-09-02 300 150 450
There are two shifts in day for a specific toll, here you can see that i have to take data from the traffic_shift_wise to traffic_day_wise, automatically just after insert in traffic_shift_wise.
So, please help me out with this. What should i use, triggers.. ?

how to update table based on another

I have 2 tables payment_scroll and virtual
virtual table contains 4 fields...
ID self_id net_amount date
1 101 600 1-1-2012
2 102 700 5-8-2012
3 103 900 13-11-2012
4 104 1100 16-9-2012
In payment_scroll table net_amount field is updated from gridview on front end. After update net_amount on same table
ID self_id net_amount date
1 101 950 3-4-2012
2 102 1100 11-6-2012
3 103 900 13-11-2012
4 104 1100 16-9-2012
I want the to update the second table payment_scroll via virtual table like the below manner.
ID self_id total_amount p1 d1 p2 d2 p3 d3 p4 d4..........upto p100 d100
1 101 5000 600 (1-1-2012) 950 (3-4-2012)
2 102 9650 700 (5-8-2012) 1100 (11-6-2012)
3 103 8000 900 (13-11-2012)
4 104 1100 1100 (16-9-2012)
please suggest me the right query to do this??

Scalar function taking so much time

I need someone who can tell me what I'm missing.
I have this scalar function in SQL Server 2008:
ALTER function [dbo].[SKU](#id1 int, #id2 int)
returns int
begin
return (
SELECT SUM(Value)
FROM Table
where id_1 = #id1
and id_2 = #id2)
end
And the table is like this:
id_1 id_2 Value
1004 1 10
1004 1 30
1004 2 100
1005 1 90
1005 1 5
1005 1 5
If I execute:
select [dbo].[SKU](1004,1)
it returns 40 - That's ok
select [dbo].[SKU](1004,2)
returns 100 - OK
select [dbo].[SKU](1005,1)
returns 100 - OK
At this point all seems ok, but my table has almost a millon rows... the result of SKU goes to the same table (update part).
But I ran it for two hours now, and is still running...
My question: I've never seen such as long time consuming query. It's ok? I'm missing something?
Thanks!, and happy new year ! D:
If changing the table design or programming to it is not an option, an easy solution would be to create a covering index on the fields you are using in your function.
Something like
CREATE INDEX IX_TABLE_ID_1_ID_2_VALUE ON dbo.Table (id_1, id_2) INCLUDE (Value)
This is not to be interpreted as an answer but an attempt to drill down to the real problem
Currently, this is as how I interpretate the actions that get executed
Starting from the initial table
id_1 id_2 Value Result
1004 1 10 NULL
1004 1 30 NULL
1004 2 100 NULL
1005 1 90 NULL
1005 1 5 NULL
1005 1 5 NULL
After update table set result = dbo.SKU(1004, 2) this would become
id_1 id_2 Value Result
1004 1 10 40
1004 1 30 40
1004 2 100 40
1005 1 90 40
1005 1 5 40
1005 1 5 40
After update table set result = dbo.SKU(1004, 1) this would become
id_1 id_2 Value Result
1004 1 10 100
1004 1 30 100
1004 2 100 100
1005 1 90 100
1005 1 5 100
1005 1 5 100
After update table set result = dbo.SKU(1005, 1) this would become (remain)
id_1 id_2 Value Result
1004 1 10 100
1004 1 30 100
1004 2 100 100
1005 1 90 100
1005 1 5 100
1005 1 5 100
and somehow after that, the result is divided by id_2
id_1 id_2 Value Result
1004 1 10 100
1004 1 30 100
1004 2 100 50
1005 1 90 100
1005 1 5 100
1005 1 5 100
Clearly, my interpretation and what really happens don't match (at least I hope so).
This might get you what you need a little quicker if you don't have to use a function.
;with sumVal
as
(
select t1.id_1, t1.id_2, SUM(t1.value) [result]
from [table] t1
group by t1.id_1, t1.id_2
)
select t2.*, s.result
from sumVal s
left join [table] t2 on s.id_1 = t2.id_1 and s.id_2 = t2.id_2
It ran in less than 5 seconds on over 800,000 rows on my test.