Mysql
 sql >> Database >  >> RDS >> Mysql

Come cercare il valore dell'intervallo dal database

Se ho capito bene, puoi usare CASE EXPRESSION in questo modo:

SELECT id,user_id,
       case when friend_points between 0 and 49 then 'Normal rate'
            when friend_points between 50 and 99 then 'Friend'
            when friend_points between 100 and 149 then 'Good friend'
            .......
       end as 'Friend_Status'
FROM profile

MODIFICA:

Oppure, se questi nomi possono cambiare dinamicamente, con un join:

SELECT t.id,t.user_id,s.name
FROM profile t
INNER JOIN friend_levels s ON(t.friend_points >= s.points_needed)
WHERE s.points_needed = (select min(f.points_needed)
                         from friend_levels f
                         where t.friend_points >= f.points_needed)