0


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' ";
1
  • 3
    The query that you have now will update more than one row if you have multiple rows where Category = Agriculture. Perhaps you could setup a SQL fiddle to illustrate your problem more clearly. Commented Nov 14, 2014 at 16:03

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.