vendredi 23 décembre 2016

Select column when IF is true

I have a scenario like this..

Meta_values table

ID |  question_id | m_id | m_value
----------------------------------
1  |  1           |  1   |  Easy 
2  |  1           |  2   |  Physics
3  |  2           |  1   |  Hard 
4  |  2           |  2   |  English

Meta_table

ID |  Meta
------------
1  |  Difficulty
2  |  Subject

I want to write a stored procedure that selects a row when Difficulty=Easy AND Subject = English. Any ideas.? Here is what i did so far

    BEGIN
    select a.q_id , a.q_text , b.answer , 
IF(c.m_id=1 AND c.m_value = d,c.m_value,
    IF(c.m_id=2 AND c.m_value=s,m_value,m_value)) as selection from question_pool as a , answers_table as b, meta_prop_values as c where a.q_id = b.q_id AND a.q_id = c.q_id    
    order by Rand() LIMIT n;
        END

Aucun commentaire:

Enregistrer un commentaire