简体   繁体   中英

Using IF ELSE in SQL recordset

So here's my code for the dynamic table in Dreamweaver

SELECT diagnose.Service_id, 
IF (diagnose.Type_id = '1',('บันทึกระบบกระดูกและกล้ามเนื้อ'), ('บันทึกแล้ว')) as Diagnose
FROM diagnose 

So the problem is how do I use the ELSE IF option if the diagnose.Type_id = 2 and so on?

To be honest, you should have this information in a reference table. I would expect the code to look more like:

SELECT d.Service_id, t.type as diagnoses_type
FROM diagnose d JOIN
     Types t
     ON d.Type_id = t.Type_id;

If you don't have such a reference table, you should build one.

You should use CASE for this, see MySQL on CASE :

SELECT diagnose.Service_id,
    (CASE diagnose.Type_id 
    WHEN 1 THEN 'lorum'
    WHEN 2 THEN 'ipsum'
    END) as Diagnose 
FROM diagnose

An alternative notation for CASE, that gives you more freedom, is:

SELECT diagnose.Service_id,
    (CASE  
        WHEN diagnose.Type_id=1 THEN 'lorum'
        WHEN diagnose.Type_id=2 THEN 'ipsum'
    END) as Diagnose 
FROM diagnose

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM