Phpmyadmin+codeigniter how can i auto update my data? - mysql

please help me i'm trying to make an auto update using phpmyadmin Trigger for my capstone title. is this correct?? i just need only to auto update in specific row only.
SELECT * FROM event
if((start_date==now())||end_date==now())
THEN
UPDATE event
SET
event_status = 'Ongoing'
WHERE
event_id = ????(Problem here);
ELSEIF((start_date < now())&&(end_date>now()))
THEN
(some code)
else if((start_date<now())&&(end_date<now()))
(some code)
ELSE
(some code)

For that you have the pseudo rows NEW and OLD, as the name suggests it has all the columns with the old and new values
if((start_date==now())||end_date==now())
THEN
UPDATE event
SET
event_status = 'Ongoing'
WHERE
event_id = NEW.event_id;
ELSEIF((start_date < now())&&(end_date>now()))
THEN
(some code)
else if((start_date<now())&&(end_date<now()))
(some code)
ELSE
(some code)

Related

toggeling with case when -SQL

I want to toggle the activate column in my table; I think it can be done with case statement! I have tried following but apparently it does not work and there is a syntax problem; could you please let me know if it can be done with one statement like the following:
update likes l case when active = 1 then set active=0 else set active=1 end where l.uuid=11 and l.scene_id=2;
Try syntax like
update likes SET
active = case when active = 1 then 0 else 1 end
where uuid=11 and scene_id=2;
Since you're using MySql, you could also do the following:
update likes
set active = !active
where uuid=11 and scene_id=2;
SQL Fiddle Demo

MySQL update column which is a value in another column

This is my previous question related to the my query.
MySQL select column which is a value in another column
The problem is that want to do operations on the values extracted and store it back into the original db. I've tried using a update & case but am not able to achieve it.
update msisdn_table
CASE reason
WHEN 'NoAnswer' THEN (case when (NoAnswer>0) then update msisdn_table set NoAnswer = NoAnswer-1 end)
WHEN 'NetworkBusy' THEN (case when NetworkBusy>0 then update msisdn_table set NetworkBusy = NetworkBusy-1 end)
WHEN 'CallRejection' THEN (case when CallRejection>0 then update msisdn_table set CallRejection = CallRejection-1 end)
WHEN 'Unavailable' THEN (case when Unavailable>0 then update msisdn_table set Unavailable = Unavailable-1 end)
END
Any help?
Try it this way if you want to do it one statement
UPDATE msisdn_table
SET NoAnswer = IFNULL(IF(reason = 'NoAnswer',
NULLIF(NoAnswer, 0) - 1, NoAnswer), 0),
NetworkBusy = IFNULL(IF(reason = 'NetworkBusy',
NULLIF(NetworkBusy, 0) - 1, NetworkBusy), 0),
CallRejection = IFNULL(IF(reason = 'CallRejection',
NULLIF(CallRejection, 0) - 1, CallRejection), 0),
Unavailable = IFNULL(IF(reason = 'Unavailable',
NULLIF(Unavailable, 0) - 1, Unavailable), 0)
WHERE reason IN('NoAnswer', 'NetworkBusy', 'CallRejection', 'Unavailable');
Note:
I changed CASE with less verbose IF(), although if you like it better you can use it the same way.
This approach has one possible side effect as it always updates the column(s) either with a new or with old value. It may matter if for example you have a trigger defined on the table.
You want to apply a WHERE clause to make sure that rows with other reason codes are not affected
Here is SQLFiddle demo
update msisdn_table set NoAnswer = NoAnswer-1 where (NoAnswer>0) ;
update msisdn_table set NetworkBusy = NetworkBusy-1 where (NetworkBusy = NetworkBusy-1) ;
update msisdn_table set CallRejection = CallRejection-1 where (CallRejection>0) ;
update msisdn_table set 'Unavailable' = 'Unavailable'-1 where (Unavailable>0) ;

SQL partially works but needs a few WHERE clauses - I think

How can I change this SQL to do the following:
If user is not in table, add them. [THIS PART WORKS]
If user IS in table AND favourite_active = 0 change it to favourite_active = 1.
If user is in table AND favourite_active = 1 change it to favourite_active = 0.
I need to add in WHERE clauses I think.
$result = mysql_query("SELECT * FROM tbl_favourites WHERE (user_id = '$who' AND favourite_id = '$usernum' AND favourite_active = '1')");
// if user does not exist in favourites, add them
if(mysql_num_rows($result) == 0)
{
mysql_query("INSERT INTO tbl_favourites (user_id, favourite_id, favourite_active) VALUES ('$who', '$usernum', '1')");
echo"You have added this user as a favourite";
}
// if user does exist and favourite_active = 1 change to 0
else {
mysql_query("UPDATE tbl_favourites SET favourite_active='0' WHERE user_id='$who' AND favourite_id='$usernum'");
echo"You have removed this user as a favourite";
}
// if user does NOT exist and favourite_active = 0 change to 1
else {
mysql_query("UPDATE tbl_favourites SET favourite_active='1' WHERE user_id='$who' AND favourite_id='$usernum'");
echo"You have removed this user as a favourite";
}
You can't have two else clauses in an IF. And you can't do the UPDATE in two separate queries, because the second one will simply undo the first one. Use the following query:
UPDATE tbl_favourites SET favourite_active = NOT favourite_active WHERE user_id = '$who' and `favourite_id = '$usernum'
I'm also not sure that the first part, which you say works, really works. Your SELECT query looks specifically for favourite_active = 1. If the user already exists with favourite_active = 0, you'll try to INSERT them. Is that really what you want? Maybe you should remove AND favourite_active = '1'.
If user_id and favourite_id form a unique index on the table, you can do the entire thing in a single query:
INSERT INTO tbl_favourites (user_id, favourite_id, favourite_active)
VALUES ('$who', '$usernum', '1')
ON DUPLICATE KEY UPDATE favourite_active = NOT favourite_active

Basic logic in MySQL

Our current mysql script that connects our Invoicing software to our website updates stock levels and what not, but there is a field in our products table which dictates if the product is visible or not which the script does not address. I want to introduce some IF logic to set the prodvisible column to 1 IF the stock level it's being updated with is > 0.
In my research, it appears that IF's cannot appear outwith functions, sadly something I have no experience of and despite my best efforts I can't get it to work.
The current script we have which works succesfully to update stock levels is as follows...
update isc_products p
set
p.prodcurrentinv =[{Level_LessOrderBook}]
where p.prodcode = '[{ItemNumber}]' and p.prodinvtrack=1
--GO;--
update isc_product_variation_combinations pvc
set
pvc.vcstock = [{Level_LessOrderBook}]
where pvc.vcsku='[{ItemNumber}]'
I want to integrate something into the first section that does something like the following
If [{Level_LessOrderBook}] > 0
p.prodvisible = 1 where p.prodcode = '[{ItemNumber}]'
ENDIF
I don't want it to set the product to invisible if it it's out of stock, just visible if it's in stock.
Thanks for any help.
You should be able to do this without an IF statement:
update isc_products p
set p.prodvisible = 1
where p.prodcode = '[{ItemNumber}]'
and [{Level_LessOrderBook}] > 0
Or, if you were asking about doing it in one statement:
update isc_products p
set
p.prodcurrentinv = [{Level_LessOrderBook}],
p.prodvisible = IF([{Level_LessOrderBook}] > 0, 1, p.prodvisible)
where p.prodcode = '[{ItemNumber}]' and p.prodinvtrack=1
Finally, how about this?
update
isc_products p
set
p.prodcurrentinv = [{Level_LessOrderBook}],
p.prodvisible = case when [{Level_LessOrderBook}] > 0 then 1 else p.prodvisible end
where
p.prodcode = '[{ItemNumber}]'
and p.prodinvtrack = 1
--GO;--
update
isc_product_variation_combinations pvc
set
pvc.vcstock = [{Level_LessOrderBook}]
where
pvc.vcsku='[{ItemNumber}]'
I formatted everything in exactly the same way as the rest of your existing script. Are you sure that the column prodvisible exists, is spelled correctly, and takes a numeric or bit value?

How do I update a table with fields selected from another table?

Although there are many questions similar to this, such as
"Updating a record from another table", but i could not get this working.
I have a query that selects and updates table sem_stdexamfinresmark. The select subquery returns multiple rows of data whose size may not be equal to the table being updated, but the update is now working.
The query looks like :
update sem_stdexamfinresmark sr,
(select
se.currsession,
str.studentid,
str.classid,
str.subjectid,
str.aggScore*(select gbtp.percentage from gb_termpercentage gbtp where gbtp.termname = se.examtype)/100 as aggPer,
str.aggGrade
from
sem_stdexamtermresr str,
sem_exam se
where
str.examid=se.examid and
se.examtype = 'Second Term' and
se.currsession =1 and classid='8'
) s
set
sr.SecondTermMark = s.aggPer and
sr.SecondTermGrade = s.aggGrade
where
sr.studentid=s.studentid and
sr.subjectid=s.subjectid and
s.currsession = s.currsession and
sr.classid='8';
EDIT:
update sem_stdexamfinresmark
set
sr.SecondTermMark = s.aggPer and
sr.SecondTermGrade = s.aggGrade
from
(select
se.currsession,
str.studentid,
str.classid,
str.subjectid,
str.aggScore*(select gbtp.percentage from gb_termpercentage gbtp where gbtp.termname = se.examtype)/100 as aggPer,
str.aggGrade
from
sem_stdexamtermresr str,
sem_exam se
where
str.examid=se.examid and
se.examtype = 'Second Term' and
se.currsession = 1 and classid='8'
) s
where
sr.studentid=s.studentid and
sr.subjectid=s.subjectid and
s.currsession =1 and
sr.classid='8';
select * from sem_exam;
update sem_exam set currsession =1;
try something that looks more like:
update foo
set col = bar.col
from bar
where ...
This is what happens when one loses sleep :( I just did a silly mistake here and added "and"