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:


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

with COALESCE and a LEFT OUTER JOIN

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

:slight_smile:

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.

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.

I’ve come up with the following as a solution to the empty string (rather than null problem):


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 :slight_smile:

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.


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


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.

change the first line as follows –

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:


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.

clear as mud :slight_smile:

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

bazz

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?

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

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.

So, I got that to work, thanks all.

Now I need to put this query inside another query. As follows:

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’ "):


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:


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

Any ideas? Thanks.