How to update single table when condition is multiple and different? - php

I want to update the table.I have two update query but I want to use it in a single update query.Column name and Where condition are different in both queries.Would you help me in this?
$sql="UPDATE points SET hero='$total_hero'+'$points' WHERE user_id='$hero_id'";
$sql="UPDATE points SET zero='$total_zero'+'$points' WHERE user_id='$zero_id'";

You can make use of the below query
$sql="UPDATE points
SET hero= CASE WHEN user_id='$hero_id' THEN '$total_hero'+'$points' ELSE hero END,
zero= CASE WHEN user_id='$zero_id' THEN '$total_zero'+'$points' ELSE zero END
WHERE user_id='$hero_id' OR user_id='$zero_id'";
Hope this would help you out.

Related

Mysqli add numeric value with concat?

I need to update a the employee hours column with the current numeric value that is already existed in the database.
$mysqli->query("UPDATE `".MYDATABASE."`.`".MYTABLE."`
SET employeeHours='".$employeeHours."'
WHERE sessionID='".session_id()."'");
This like is working properly but needless to say that it's overwriting the old value.
My goal is to add the current $employeeHours to the existed value in the database.
I assume that it's done with concat but I'm not sure about the syntax.
Small tweak needed here...
I couldn't believe that it would be so ridiculously simple :
$mysqli->query("UPDATE `".MYDATABASE."`.`".MYTABLE."` SET employeeHours = employeeHours + '".$employeeHours."' WHERE sessionID='".session_id()."'");
Thank you #castis!

Set all rows in SQL where id='$id'

I have made an online Police Dispatching program and I am trying to add a new feature.
I want to add a button to set all Police Officer's status to 'On Duty'.
This is my current action for the form/submit button:
<?php
mysqli_query($con,"UPDATE users SET status='0' WHERE code='$code'");
mysqli_close($con);
printf("<script>location.href='../units.php'</script>");
?>
The 'On Duty' is the same as 0, busy is 1, and unavailable is 2. The code is basically the Police Department's unique code that all Officers have.
Right now, this is affecting any rows! Please help!
If you want ALL to be set to Zero, as you say, just Drop your WHERE statement.
"UPDATE users SET status='0'";
You should drop the Single Quotes as well, but it's not hurting anything.
"UPDATE users SET status=0";
Database has this security that don't allow users to update all data when you are using a UPDATE statement without WHERE clause, you can do a hacky query like this
UPDATE users SET status = 0 WHERE code != 'any value'; // if code is string
UPDATE users SET status = 0 WHERE code <> 0; // if integer, just makes sure 0 is not a code in your DB
all rows that didn't match in the value will be updated, in this case, all rows will be updated

Update two different strings in one column in mysql

Good day, I have a simple question here where I want to update my column whenever these two value are used.
UPDATE products SET description='YES' WHERE description='PENDING'
other than 'PENDING', I also want 'NO' to be included in this query for update.
What can I do? I want it to be updated on any row I update/click, Thank you.
update products
set description =
case
when description='PENDING' then 'YES'
else 'NO'
end
Are you looking for st. like this?
UPDATE products
SET description='YES'
WHERE description IN ('PENDING','NO') AND
id = 3
It sets description="YES" to all rows where description is equal to PENDING or NO.
try below:
UPDATE products SET description=if(<Your Condition to set Value YES>,'YES','NO') WHERE description='PENDING'

mysql update query on two tables

I am trying to update two tables in one query using a query that looks like this:
$query = "UPDATE tblproducts, tblsideimages
SET tblproducts.prodCode='oj', tblsideimages.prodCode='oj'
WHERE tblproducts.prodCode='aj'
AND tblsideimages.prodCode='aj'";
It works if there are contents in tblsideimages such as that one but it doesn't work if tblsideimages has no contents.
The problem is that I have to make it adjust accordingly because it is not always that there will be a content in tblsideimages. Sometimes the user can add data that is inserted in tblproducts only. How can I make it that, if there is no content in tblsideimages, it will still work for tblproducts and if tblsideimages and tblproducts both has content, both will update. Thanks!
There is no way (and real reason) to do all the work in one query. So just split it into:
UPDATE tblproducts SET prodCode='oj' WHERE prodCode='aj'
UPDATE tblsideimages SET prodCode='oj' WHERE prodCode='aj'
Less queries doesn't mean "more performant", so never follow the idea to fit everything into one query.
you could do this with a stored procedure - write a stored procedure with the update statements in a transaction.
Using rollbacks, you can ensure that they are treated as one unit of work, ie either they are all executed or none of them are, to keep data consistent.
or execute multiple queries like
$query="UPDATE tblproducts
SET tblproducts.prodCode='oj'
WHERE tblproducts.prodCode='aj';
UPDATE tblsideimages
SET tblsideimages.prodCode='oj'
WHERE tblsideimages.prodCode='aj'";

adding to numerical value of column

Hi so I was wondering what the best way to add or subtract from a field in my table would be. The way I know it would work is if I query the value do the addition and then UPDATE the value.
I would rather include the addition as part of the update query like:
UPDATE users SET points = +10
Or something like that. Is it possible?
You just need to name the column on the right hand side of the = operator:
UPDATE `users` SET `points` = `points`+10
Since there is no WHERE clause this will give all users 10 more points then they currently have.
You can just do this:
UPDATE users SET points = points + 10

Categories