0

The problem I'm having is with a strategy browser game which has 7 types of values. The problems are as follows:

  1. I got 7 different values in $_POST, from index unit_1 to index unit_7 included. These 7 values are INTEGERS between 0 and 20 and aren't static. They can at each call of the page. They represent the number of units sent.
  2. I got 7 lines in a table that does not follow the same schema : "nameUnit" = "Soldier" && nbUnit = "0", "nameUnit" = "Mage" && nbUnit = "5", etc..
  3. I need to do an update on the number of units in 1 request, but I don't know how to do this.

Example of what I want to do :

UPDATE x SET nbUnit = $_POST['u1'] WHERE nameUnit = "Soldier", nbUnit = $_POST['u2'] WHERE nameUnit = "Mage" [...]

Is it possible to do this that way and can it be done in a single query?

3
  • If the values updated (SET clause) are the same, you can use the clause OR. If the values are different, you have the create one update for each statement. Commented May 11, 2018 at 13:22
  • Check MySQL CASE dev.mysql.com/doc/refman/5.7/en/case.html Commented May 11, 2018 at 13:24
  • Worked with CASE. Thanx you. Commented May 11, 2018 at 13:50

2 Answers 2

1

this work in ur case

UPDATE x 
SET nbUnit = $_POST['u1'] 
WHERE (nameUnit = "Soldier" AND nbUnit = $_POST['u2']) OR 
(nameUnit = "Mage" AND nbUnit= $_POST['u5'])
Sign up to request clarification or add additional context in comments.

Comments

0

IN PL SQL you can do the following :

UPDATE x SET nbUnit = case when nameUnit = "Soldier" then $_POST['u1'] 
when nameUnit = "Mage" then $_POST['u2'] else nbUnit end;

3 Comments

Your solution seems to work perfectly. Is this possible to do "THEN nbUnit + $_POST['u1']" to increment ?
after then expression comes which can represent a value as mentioned in Oracle : In a simple CASE expression, Oracle Database searches for the first WHEN ... THEN pair for which expr is equal to comparison_expr and returns return_expr. If none of the WHEN ... THEN pairs meet this condition, and an ELSE clause exists, then Oracle returns else_expr. Otherwise, Oracle returns null. You cannot specify the literal NULL for every return_expr and the else_expr.
I understand. I got this working perfectly using CASE. For information, I used to do UPDATE x SET y = CASE [...] ELSE y END WHERE z = xxxx, and it seems to work. It must be way better than using AND on a WHEN clause. Thanx for everything !

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.