mysql中有多个ifs

时间:2014-12-08 21:48:55

标签: mysql if-statement

我有点困惑于我一直在研究的一个查询。我需要知道我是否可以使用多个ifs(即如果x和y为真,那么z)

SELECT selling_company_id
     , rfp_id
     , offer.id
     , vendor
     , if (company_d.offer.vendor in (select companyName from company.SellingCompanyTable), '1'
     , if (company_d.offer.vendor = '', '0', '0.5')) as 'Vendor Mult'`

情况:我需要从批准这些供应商的另一个表中返回供应商名称(isApproved ='t')

我非常感谢任何反馈或想法。感谢

2 个答案:

答案 0 :(得分:0)

也许尝试加入表格,然后对它做条件。也许是这样的?

SELECT o.selling_company_id
     , o.rfp_id
     , o.id
     , o.vendor
     , if(c.id is not NULL, 1, 
            if(o.vendor = '', '0', '0.5')
       ) as 'Vendor Mult'
FROM company_d.offer o
LEFT JOIN company.SellingCompanyTable c ON c.companyName = o.vendor

答案 1 :(得分:0)

SELECT
    selling_company_id,
    rfp_id,
    offer.id,
    vendor,
    IF (company_d.offer.vendor in (SELECT companyName FROM company.SellingCompanyTable),
        '1', IF (company_d.offer.vendor = '', '0', '0.5')) AS 'Vendor Mult',
    format,
    CASE company_d.offer.format WHEN '' THEN '0'
FROM company_d.offer
LEFT JOIN company_d.rfp
ON company_d.rfp.id = company_d.offer.rfp_id
WHERE company_d.offer.buying_company_id = 12565
AND last_modified_at BETWEEN '2014-7-01' AND '2014-12-31';