SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SQL query with IF

    Is it possible to put a subquery within an IF?
    From my testing it would seem "no" would be the answer.

    I have tried the following:
    Code SQL:
    SELECT 
    IF (pr_si_link != "", pr_si_link, SELECT pr_si_link FROM prsida WHERE product_id = 1 AND site_id = 1 ), link_status 
    FROM 
    prsida WHERE product_id = 1 AND site_id = 2

    Basically the above is meant to pull a link code for site_id = 2, but if that link is empty "", is uses the default link code (site_id = 1)

    If I replace the inner SELECT with some hardcoded value the query executes, but as soon as I add the inner SELECT it breaks.

    How else would you suggest doing this?

    Thanks

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,265
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by LuckyB View Post
    How else would you suggest doing this?
    with COALESCE and a LEFT OUTER JOIN
    Code:
    SELECT COALESCE(optl.pr_si_link
                   ,dflt.pr_si_link) AS pr_si_link 
      FROM prsida AS dflt -- default
    LEFT OUTER
      JOIN prsida AS optl -- optional
        ON optl.product_id = dflt.product_id
       AND optl.site_id = 2
     WHERE dflt.product_id = 1
       AND dflt.site_id = 1
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, thanks for your reply. But I don't think that works.
    It seems to return the empty pr_si_link (site_id = 2) where it is supposed to check the pr_si_link for site_id = 2, and return the pr_si_link from site_id = 1 (the default) if (and only if) the site_id = 2 link is blank. if site_id (2) link is NOT blank, return it ....If the site_id (2) link is blank, return link from site_id (1),...
    So programmitically thinking:
    if (site_id == 2 && pr_si_link == "") {
    //return link for site_id = 1 (the default)
    } else {
    //return link for site_id = 2 (as it is isn't empty)
    }

    hmmm, hope that makes sense.

  4. #4
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ive never heard of COALESCE until now. Just G'd it and it seems to be referring to non-null. The field isn't NILL but just empty "". So that is considered not null too.

  5. #5
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've come up with the following as a solution to the empty string (rather than null problem):
    Code MySQL:
    SELECT COALESCE(IF(optl.pr_si_link="",NULL,optl.pr_si_link)
                   ,dflt.pr_si_link) AS pr_si_link, opt1.link_status 
      FROM prsida AS dflt -- default
    LEFT OUTER
      JOIN prsida AS optl -- optional
        ON optl.product_id = dflt.product_id
       AND optl.site_id = 2
     WHERE dflt.product_id = 1
       AND dflt.site_id = 1

    please let me know if you see any problems with it, or got a better way

    Except I get an error "Unknown column 'opt1.link_status' in 'field list'"
    I need to get the link_status from the non-default linking code.

  6. #6
    SitePoint Guru
    Join Date
    Jan 2004
    Location
    Uppsala, sverige
    Posts
    697
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Code:
    select case when pr_si_link <> '' then  pr_si_link
                else (select pr_si_link 
                        from prsida 
                       where product_id = 1 
                         and site_id = 1) end,
           link_status
      from prsida 
     where product_id = 1 
       and site_id = 2

  7. #7
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Code:
    SELECT site_id, pr_si_link
    FROM prsida
    WHERE product_id = 1
    AND
      ( (site_id = 2 AND pr_si_link <> '') OR
        (site_id = 1)                            )
    ORDER BY site_id DESC
    I didn't test it, but the first row it returns should contain the link you're interested in.

  8. #8
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,265
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by LuckyB View Post
    I've come up with the following as a solution to the empty string (rather than null problem):
    Code:
    SELECT COALESCE(IF(optl.pr_si_link="",NULL,optl.pr_si_link)
                   ,dflt.pr_si_link) AS pr_si_link ...
    change the first line as follows --
    Code:
    SELECT COALESCE(NULLIF(optl.pr_si_link,'')
                   ,dflt.pr_si_link) AS pr_si_link ...
    NULLIF is used specifically for instances like this, where you want a specific value to be treated as NULL
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  9. #9
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    change the first line as follows --
    Code:
    SELECT COALESCE(NULLIF(optl.pr_si_link,'')
                   ,dflt.pr_si_link) AS pr_si_link ...
    NULLIF is used specifically for instances like this, where you want a specific value to be treated as NULL
    Thanks, trying that:
    Code MySQL:
    SELECT COALESCE(NULLIF(optl.pr_si_link,'')
                   ,dflt.pr_si_link) AS pr_si_link, opt1.link_status 
      FROM prsida AS dflt -- default
    LEFT OUTER
      JOIN prsida AS optl -- optional
        ON optl.product_id = dflt.product_id
       AND optl.site_id = 2
     WHERE dflt.product_id = 1
       AND dflt.site_id = 1

    Now I get the following error: Unknown column 'opt1.link_status' in 'field list'

    And, yes, link_status does exist in prsida. Need to get it from the "product_id = 2" row, no matter what.

    And thanks for the other suggestions too guys.

  10. #10
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by LuckyB View Post
    Need to get it from the "product_id = 2" row, no matter what.
    clear as mud

    can you show your create table statements and some sample data for each? I can't visualise where the data sits.

    bazz

  11. #11
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,265
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by LuckyB View Post
    Now I get the following error: Unknown column 'opt1.link_status' in 'field list'.
    ah, i guess that was a poor choice for the table alias name

    i named it oh-pee-tee-ell (optional)

    you typed oh-pee-tee-one (opt one)

    see the difference?

    Quote Originally Posted by LuckyB View Post
    And, yes, link_status does exist in prsida. Need to get it from the "product_id = 2" row, no matter what.
    no matter what... provided that the product-id 2 row actually exists

    i mean, that's why we're using a LEFT OUTER JOIN, for cases when it doesn't exist -- and in those cases, link_status will be NULL
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  12. #12
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    ah, i guess that was a poor choice for the table alias name

    i named it oh-pee-tee-ell (optional)

    you typed oh-pee-tee-one (opt one)

    see the difference?
    LOL. Thanks so much. I feel like a fool now. I looked at it so long, and didn't even notice it was an "l" and not "1". haha Thanks again.

  13. #13
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, I got that to work, thanks all.

    Now I need to put this query inside another query. As follows:
    Code MySQL:
    SELECT p.product_id, p.pr_label as label,p.pr_slug as slug,p.pr_status as product_status, psd.pr_si_link as link, psd.link_status, 
     
    psd.pr_si_des_sh as short_description, psd.pr_si_des_lo as long_description FROM product p, (
     
    SELECT COALESCE(NULLIF(optl.pr_si_link,'')
                   ,dflt.pr_si_link) AS pr_si_link, optl.link_status, optl.pr_si_des_sh,optl.pr_si_des_lo 
      FROM prsida AS dflt -- default
    LEFT OUTER
      JOIN prsida AS optl -- optional
        ON optl.product_id = dflt.product_id
       AND optl.site_id = 2
     WHERE dflt.product_id = 1
       AND dflt.site_id = 1
     
    ) AS psd WHERE p.product_id = 1
    The above works, but I want to change the subquery so that the product_id = 1 part. Something like the following (which doesn't work " Unknown column 'p.product_id' in 'where clause' "):
    Code MySQL:
    SELECT p.product_id, p.pr_label as label,p.pr_slug as slug,p.pr_status as product_status, psd.pr_si_link as link, psd.link_status, 
     
    psd.pr_si_des_sh as short_description, psd.pr_si_des_lo as long_description FROM product p,(
     
    SELECT COALESCE(NULLIF(optl.pr_si_link,'')
                   ,dflt.pr_si_link) AS pr_si_link, optl.link_status, optl.pr_si_des_sh,optl.pr_si_des_lo 
      FROM prsida AS dflt -- default
    LEFT OUTER
      JOIN prsida AS optl -- optional
        ON optl.product_id = dflt.product_id
       AND optl.site_id = 2
     WHERE dflt.product_id = p.product_id
       AND dflt.site_id = 1
     
    ) AS psd WHERE p.product_id = 1

    The difference being the subquery WHERE clause:
    WHERE dflt.product_id = 1
    WHERE dflt.product_id = p.product_id

    The reason I want to change this is ultimately as I build the query up I want the outer query WHERE clause to be something like WHERE p.product_id IN (some subquery here):
    eg:
    Code MySQL:
    SELECT p.product_id, p.pr_label as label,p.pr_slug as slug,p.pr_status as product_status, psd.pr_si_link as link, psd.link_status, 
     
    psd.pr_si_des_sh as short_description, psd.pr_si_des_lo as long_description FROM product p, (
     
    SELECT COALESCE(NULLIF(optl.pr_si_link,'')
                   ,dflt.pr_si_link) AS pr_si_link, optl.link_status, optl.pr_si_des_sh,optl.pr_si_des_lo 
      FROM prsida AS dflt -- default
    LEFT OUTER
      JOIN prsida AS optl -- optional
        ON optl.product_id = dflt.product_id
       AND optl.site_id = 2
     WHERE dflt.product_id = p.product_id
       AND dflt.site_id = 1
     
    ) AS psd WHERE p.product_id IN (some subquery here)

    Thanks

  14. #14
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any ideas? Thanks.


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
  •