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'
Related
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.
Is there a way that I could update several rows based on a column?
I have a table called category with 2 columns (Category, number).
The column category have 2 rows(Agriculture,Apparel) which is fixed. I need a mySQL update statement that could update the number column. That means 2 rows in 'number' column will have to be updated.
The code that I gave could only update one row at the time. How do I update both rows using a single query? Thanks.
My code:
$sql="UPDATE category
SET number = '$AF'
WHERE Category = 'Agriculture' ";
Not quite sure I'm understanding. If you want to update all of the rows, then remove your where clause. SQL is very much a "the less you put in the query, the more you get"-type language.
If you want to change the values of the number field to DIFFERENT values, but in a single query, then you have to do a very ugly:
UPDATE category
SET number = CASE Category
WHEN 'Agriculture' THEN $value_for_agriculture
WHEN 'Apparel' THEN $value_for_apparel
END CASE
Let say I have something like this:
id | title |
1 | First row |
Now I want to update that value to be: First row is here, by just adding is here. And as you think, there is more than one row, and I want to update all the rows dynamically.
UPDATE posts SET title=title+' is here'
I know that the above is wrong, I just thought that since it works with numbers, maybe it will also with text, but it doesn't.
To do that you need to concatenate strings, MySQL has the function CONCAT(), so your query would be:
UPDATE posts SET title=CONCAT(title,' is here')
UPDATE posts SET title=CONCAT(title,' is here')
use concat:
UPDATE posts SET title=concat(title,'your_string_to_add') WHERE id='your_id'
It is important to give WHERE id = 'id' otherwise it will update the first row I beleive. In your case:
UPDATE posts SET title=concat(title,' is here') WHERE id=1
I have a question about inserting row order number by spesific order type.
Products table has OrderNumber field. I want to programaticly add new line to appropriate OrderNumber by its name. If the reference column would be integer it has to be easy like that
update products set OrderNumber=OrderNumber+1 where Price>555
Is there similar way for varchar field like
update products set OrderNumber=OrderNumber+1 where Name>'bla%'
Thank you
You can use STRCMP('text', 'text2')
update products
set OrderNumber=OrderNumber+1
where STRCMP(Name, 'bla') = 1;
I missunderstood your point. Can you try something like this?
SET #rownum:=0;
update
set OrderNumber=#rownum:=#rownum + 1
from products
order by Name;
You can simply run an update query with a sequential number like in here
Sounds like a bad design. Why not simply have a plain-jane auto_increment field and order by that? Every new record would by defnition have a higher ID than any of its predecessors.
you mean something like update products set OrderNumber=OrderNumber+1 where Name like 'bla%'
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