SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2003
    Location
    Melbourne, Australia
    Posts
    463
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    run different command if sql returns null

    hi all
    is it possible to have an sql statement
    that will SELECT * FROM something WHERE something
    but if nothing is returned, do another SELECT statement
    just from within the SQL
    rather than having to use php to check
    and then run the sql statement again

    does that make sense?

    thanks
    dave

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,262
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    not the way you've explained it, no

    however, if "do another select" is supposed to go against the same table, maybe there's a workaround

    can you give examples of what you want?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2003
    Location
    Melbourne, Australia
    Posts
    463
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your reply
    eg would be
    SELECT headline FROM news WHERE id = somenumber
    but if there is no record for that, then just do
    SELECT MAX(headline) FROM news

    i'm thinking of something more complicated, but i don't want the complex nature of the sql
    to get in the way of the simple question

    thanks

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,262
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    this should work in 4.0+
    Code:
    (
    SELECT 1 as seq,
         , headline 
      FROM news 
     WHERE id = somenumber
     )
    union all
    (
    SELECT 2
         , MAX(headline) 
      FROM news
    )
    order by seq
    limit 1
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Evangelist
    Join Date
    Mar 2003
    Location
    Melbourne, Australia
    Posts
    463
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, i worked out the way i really wanted it just using LEFT JOIN
    but i'll save your note
    is that a transaction?

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,262
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    transaction? no, it's a union query
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  7. #7
    SitePoint Evangelist
    Join Date
    Mar 2003
    Location
    Melbourne, Australia
    Posts
    463
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry i thought that's what transactions were in mysql


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
  •