-2

Well, that's hell of a question.

Here is in a way what I would like

$result = mysql_query(" SELECT (less, more) FROM tempTable WHERE id = '$id' ");
    $row = mysql_fetch_row($result);

    if( ($row[1] - $row[0]) / 75  <= 1.5 )
    {
        INSERT INTO tempTable (payValue) VALUES ('$ammount') WHERE id = '$user';        
    }

Question is, is it possible to make one of these two queries?

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
user3316068
  • 47
  • 1
  • 3

1 Answers1

1

Yes, you can do this in one statement:

INSERT INTO tempTable (payValue)
    SELECT $ammount
    FROM tempTable
    WHERE id = '$id' and (more - less) / 75 <= 1.5;

EDIT:

Well, this is probably another situation where an insert should really be an update:

UPDATE tempTable
    set payValue = $ammount
    WHERE id = '$id' and (more - less) / 75 <= 1.5;

Use insert when you want to add new rows. Use update when you want to change the values of existing rows.

If you do want another row, then it would be something like:

INSERT INTO tempTable (id, payValue)
    SELECT id, $ammount
    FROM tempTable
    WHERE id = '$id' and (more - less) / 75 <= 1.5;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Well, if I put `INSERT INTO tempTable (payValue) SELECT 85 FROM tempTable WHERE id = 0 and ( more - less) / 75 <= 1.5; ` in the left pannel in : http://sqlfiddle.com/#!2/960a1/1 I got an error ( which is probably due to SQLFiddle and not MySQL itself ) : Field 'id' doesn't have a default value – user3316068 Feb 16 '14 at 15:12
  • That did the trick. Two times today, thanks alot Gordon! You clearly made my day! – user3316068 Feb 16 '14 at 15:20