Can I construct a query this way?

Discussion in 'MySQL' started by Tony Brar, Oct 18, 2012.

  1. #1
    Hi guys,

    Can I construct a query like this?
    DELETE * FROM table WHERE column < numbervalue - number
    If not, how would I accomplish something like this?

    Thanks,
    Tony
     
    Solved! View solution.
    Tony Brar, Oct 18, 2012 IP
  2. Gemba

    Gemba Member

    Messages:
    36
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    25
    #2
    Yes, you can.
    You should put the (numbervalue - number) in brackets to be sure, but I'm pretty sure it works without them.
     
    Gemba, Oct 19, 2012 IP
  3. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #3
    Thanks! If anyone else has any input, it is welcome.
     
    Tony Brar, Oct 26, 2012 IP
  4. iliveforyou

    iliveforyou Active Member

    Messages:
    207
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #4
    DELETE FROM table WHERE column < 10;
     
    iliveforyou, Oct 28, 2012 IP
  5. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #5
    That's not what I meant. I meant like DELETE FROM table WHERE column < var minus var2

    -Tony
     
    Tony Brar, Oct 28, 2012 IP
  6. #6
    The database will evaluate (var - var2) as a single value, so it works the same as if you said column < 10.
     
    Rukbat, Oct 28, 2012 IP
  7. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #7
    thanks
    -tony
     
    Tony Brar, Oct 28, 2012 IP
  8. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #8
    What if I use PHP?
    Do I still have to use quotes like this:
    SELECT * FROM table WHERE column = ('$foo' - '$bar')

    -Tony
     
    Tony Brar, Oct 29, 2012 IP
  9. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #9
    Since this is in PHP, you follow PHP rules for generating a string, not SQL rules for generating a query. So it's

    $query = "SELECT * FROM table WHERE column = '".$foo - $bar."';";

    This is assuming that "column" is text and $foo and $bar are numeric. If "column" is numeric, you don't quote (it's not optional - it will throw an error if you do quote), so

    $query = "SELECT * FROM table WHERE column = ".$foo - $bar.";";

    If $foo and $bar are non-numeric, $foo - $bar doesn't make any sense, so there's no query you can make with them unless you want to delete the string in $bar from the string in $foo - which requires string manipulation, not arithmetic.

    IOW, PHP strings have to be quoted. SQL string values have to be quoted, but SQL numeric values can't be quoted. (You can use " or ' in PHP to quote a string. In "", a variable will be evaluated, and replaced with its value. In '', variables will be the literal string name of the variable, i.e., $foo will be $foo, not the value of $foo. If you want ' as a character in the string, use " as the quotation mark for the string. "'" is a string containing one single quote.)

    All the PHP stuff about string quoting is in the PHP manual at http://php.net/manual/en/language.types.string.php

    (For SQL quoting, see any SQL tutorial.)
     
    Rukbat, Oct 30, 2012 IP
  10. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #10
    THIS REFERS TO COLUMN AND VALUES AS NUMERIC!!!
    If they are ALL numeric, I can just do this, I guess:
    $val = $foo-$bar;
    mysql_query("SELECT * FROM table WHERE column > $val");

    I suppose this question didn't need to be asked, the fix is one extra line of code.
    -Tony
     
    Tony Brar, Oct 31, 2012 IP