Joshua John
Joshua John

Reputation: 21

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?

Upvotes: 0

Views: 70

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269633

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.

Upvotes: 1

rolfv1
rolfv1

Reputation: 571

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

Upvotes: 0

Related Questions