mardi 19 avril 2016

multiple if in MySQL

I had SQL a few years ago, but somehow I can't remember how to solve this problem anymore, and since I tried for so long I thought I should just ask here, because I feel like the solution is really simple.

I have a table called players like this:

<table width="100%" border="1" cellpadding="0" cellspacing="2">
 <tr>
  <td>name</td>
  <td>birthday</td>
  <td>attribute_1</td>
  <td>attribute_2</td>
  <td>value</td>
 </tr>
 <tr>
  <td>Mike</td>
  <td>1992-05-11</td>
  <td>57</td>
  <td>60</td>
  <td>xxx</td>
 </tr>
 <tr>
  <td>Max</td>
  <td>1980-12-07</td>
  <td>40</td>
  <td>22</td>
  <td>xxx</td>
 </tr>
 <tr>
  <td>Tom</td>
  <td>1986-02-10</td>
  <td>30</td>
  <td>30</td>
  <td>xxx</td>
 </tr>
</table>

I now want to add a value to every person based on their attribute 1, 2 and their age like follows: attribute_1 should be weighted with 70%, attribute_2 with 30%. If someone is 27 or younger, than just weight both attributes as showed above and do nothing else. If someone is older than 27 but younger than 32 than subtract the value 5 to the result from above. If someone is older than 31 subtract the value 10 to the result. Now if the value would drop to zero or below (because his attributes 1 and 2 are below 10 and he is older than 32 for example) than set the value to 2.

I already fail with one if-clause, so I don't really now what to change here:

SELECT IF (TIMESTAMPDIFF(YEAR, birthday, NOW() <= 27)
SET value = ((attribute_1/100*70) + (attribute_2/100*30))
ELSE set value = 2;

Any idea where my fault is? Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire