dcsimg
www.webdeveloper.com
Results 1 to 1 of 1

Thread: MySQL conditional OR

  1. #1
    Join Date
    Feb 2011
    Location
    Waterloo, Ontario, Canada
    Posts
    80

    MySQL conditional OR

    I have a fairly straightforward problem:

    I have a stored procedure that queries based on parameters. For example:

    CREATE PROCEDURE `EXAMPLE_PROC`(IN v_From DATETIME, IN v_To DATETIME, IN v_ShowId INT)
    BEGIN
    SELECT col1, col2 FROM myTable
    WHERE Date >= IFNULL(v_From, NOW()) AND Date <= IFNULL(v_To, '2150-01-01')
    END

    V_ShowId is most often going to be NULL. However, when it's not, I need to be able to do:

    SELECT col1, col2 FROM myTable
    WHERE (Date >= IFNULL(v_From, NOW()) AND Date <= IFNULL(v_To, '2150-01-01')) OR ShowId = v_ShowId

    What's the easiest way I can do this?

    I'm assuming a simple solution might be to:

    IF v_ShowId IS NOT NULL THEN
    [run query without OR]
    ELSE
    [run query with OR]

    but my actual query is quite lengthy ~ 40ish lines, so duplication would make this whole thing ~80 lines. Any tips?


    EDIT: It must be getting late.. Having the OR in there already does what I want it to. When ShowId = v_ShowId, it returns that row regardless if it passed the other conditions or not and if it doesn't then then it has no effect. The thread can be deleted or used as a cautionary tale to not work when you're tired!
    Last edited by Galway; 08-02-2013 at 11:50 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles