SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)

    Math in the Query - Can I Do This?

    Let me start off by saying that my SQL queries have been pretty simple since I started coding. When I say simple, I mean that I just select a bunch of information from static inputs. Now, I've come across a situation where I need to do a calculation within the query itself. Something like this:

    SELECT * FROM Table WHERE Table.Rent <= ($Amount * Table.Rooms)

    Rent is a column of the table and is a numeric value.
    $Amount is an input provided by the user and is a numeric value.
    Rooms is a column of the table and is an integer.

    I guess I could first query the number of Rooms and do the math with some PHP code before I run a second query but that just seems like a lot of work. Anyway, if I can do math, what's the syntax? Just need an example and I can go form there.
    TAKE A WALK OUTSIDE YOUR MIND.

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by JeffWalden View Post
    Anyway, if I can do math, what's the syntax?
    yes you can

    the syntax is pretty much exactly what you have shown

    test it, and see
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Pretty much? I'll throw some quotes in there and see. Guess my initial testing bombed out so I wasn't sure if something like this was even possible. Now that I know there's a light at the end of the tunnel I'll get to work.

    Cheers!
    TAKE A WALK OUTSIDE YOUR MIND.

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    quotes?

    we ain't got no quotes... we don't need no quotes... i don't have to show you any stinkin' quotes!!
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    we ain't got no quotes... we don't need no quotes... i don't have to show you any stinkin' quotes!!
    Rudy, where did this come from?!

    I think I managed to get it working. It's amazing what happens when I spell the table name correctly!
    TAKE A WALK OUTSIDE YOUR MIND.

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by JeffWalden View Post
    Rudy, where did this come from?!
    treasure of the sierra madres with humphrey bogart

    http://rudy.ca/badges.wmv (1M)

    if this launches for you in windows media player, it should loop, and it gets pretty funny after it has repeated about a dozen times...
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  7. #7
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Guess that was a smidge before my time.

    ... still funny.
    TAKE A WALK OUTSIDE YOUR MIND.

  8. #8
    SitePoint Evangelist
    Join Date
    Sep 2006
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's a funny clip!!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •