MySQL Event - Update When - mysql

I have a table with 7500+ records and every day is inserted about 300 new ones. Each new record can belong to a particular partner where each partner has its own idpartner.
Example of records: (partner A -> idpartner: 1 / partner B -> idpartner: 2 / partnerC -> idpartner: 3)
PartnerA-123
PartnerA-567
PartnerB-999
PartnerB-123
PartnerC-123
What I can not do is an event that runs every 10 minutes which will update the idpartner based on what comes before "-". If it's partnerA-, then idpartner = 1 and so on ...
I'm having to run an update every 10 minutes to keep this updated ... What I do:
update table set idpartner = 1 where name regexp 'partnerA-';
update table set idpartner = 2 where name regexp 'partnerB-';
update table set idpartner = 1 where name regexp 'partnerC-';
How to make an event that update the table every 10 minutes based on names with regexp?

Related

Update Multiple Rows in MySQL With Similar Content

I have an event table. (I didn't create this table)
The fields are id, event_id, start_date, end_date, e_status
The only thing that is not unique is the id. The rest are the same on all rows.
How do I update the status?
I tried:
UPDATE events
SET e_status =
CASE event_id
WHEN 12830 THEN 0
END
WHERE start_date = '2016-06-24 17:30:00'
AND end_date = '2016-06-24 18:00:00'
AND event_id IN (12830)
No updates were changed.
before editing
ID Event Status
1 USA 0
2 UK 0
3 Canada 0
Your simple query would be
update event set status=1
After Editing
ID Event Status
1 USA 1
2 UK 1
3 Canada 1
and if you wants to target specific record just use where clause
update event set status=1 where id=1
If all the data is the same on all rows except for the id column, you shouldn't need any criteria in the WHERE clause. You should be able to just have this:
UPDATE events
SET e_status =
CASE event_id
WHEN 12830 THEN 0
END
And actually, you don't even need the case statement. If all data is the same except for id, then won't all rows have an event_id = 12830? You can just have ==>
UPDATE events
set e_status = 0

update value in comma separated and replace with value in query

iam having table like this
id name sol_id
1 abc 2,5,8
2 dt 5,9,10
here i want to add some value(10) to id=1 of sol_id,so value 10 will be added with id=1 and at the same time value 10 of id=2 replace with some empty value i want output like this
id name sol_id
1 abc 2,5,8,10(here updating)
2 dt 5,9 (10 removing)
i wrote query like this but its performs one operation not both
UPDATE my_table SET sol_id=REPLACE(sol_id,',10,',',')
and sol_id = Concat(sol_id, ',', 10) where id = 1
is it possible? Thanks in advance
It is possible, but clunky.
Basically you would do
UPDATE table SET SET field = CASE id
WHEN 1 THEN <formula for the case of id=1>
WHEN 2 THEN <formula for the case of id=2>
END
WHERE ID IN (1, 2);
There's no great advantage over running several queries inside a TRANSACTION and, if necessary, a suitable lock on the table.

updating the column of a table with different values

I have a table called person there are about 3 million rows
I have added a column called company_type_id whose default value is 0
now i want to update the value of company_type_id to 1
where person_id from 1 to 212465
and value of company_type_id to 8 where person_id from 256465 to 656464
how can i do this
I am using mysql
You can do that in one update statement:
update person
set company_type_id = 1
where
(person_id >= 1 and person_id <= 212465) or
(company_type_id = 8 and person_id >= 256465 and person_id <= 656464)
update person set company_type_id=1 where person_id>=1 and person_id<=212465;
I am sure you'll make the second update query by yourself.
Two SQLs:
1:
UPDATE person
SET company_type_id = 1
WHERE person_id BETWEEN 1 AND 212465
2:
UPDATE person
SET company_type_id = 8
WHERE person_id BETWEEN 256465 AND 656464

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.

mysql update between row and shift current to right

how to update column value of specific id and shift after to right.
id track
1 3
2 5
3 8
4 9
want to update id 3 track column value to 10, result like this
id track
1 3
2 5
3 10
4 8
5 9
id column is auto_increment
or any suggestion it's my pleasure.
thank you.
You should avoid tweaking auto_increments. Auto increment keys are usually supposed to be used internally (e.g. for linking purposes). If you want to order tracks, i suggest you add a seperate numeric field "ordernro" to the table and update that
To add a column order nro to a table named album, do like this:
alter table album add ordernro int(2) after id;
Then copy the current value for id into this new column:
update album set ordernro=id;
(do this only once after adding the column)
To insert track 10 at position 3 first shift the rows:
update album set ordernro = ordernro + 1 where ordernro >= 3;
And then insert track 10:
insert into album (ordernro, track) values (3, 10);
Remember to update your existing insert/update/select statements accordingly.
The result can be checked by:
select * from album order by ordernro;
(The id will now be "mixed up", but that doesn't matter)
UPDATE table SET id = id + 1 WHERE id >= x;
x being the id where you place your current track.
The problem with JK 's answer is that MySQL returns error saying that is can't UPDATE because the index at x+1 would be duplicate.
What I did is
UPDATE table SET id = id + 100 WHERE id >= x;
UPDATE table SET id = id - 99 WHERE id >= x;
And then INSERT my row at index x