php - mysql IF conditioned by another field -
i have following simple inbox simulated query (for mail purposes)
select * inbox receiver_id=:receiver_id , receiver_type='a' , spam!='y' order date desc, time desc limit :paging offset :offset
inbox table has regular fields such subject, mail_body, date, time etc. contains flag (sender_type) can "c", "l", "a" , (sender_id)
i want include extra field @ end of (select *) named ext contains extension of image particular type of user (so can display image - image format id, ext meaning $id . $ext - 1.jpg, 2.png etc....
i think doing 1 query better now, main query above, , each entry scan particular table type c, l , users, , requesting corresponding ext particular id (sender_id) can display picture.
would useful (and possible) add ext extension of image of each type of user in above select ? assume it's done if statement, don't know how. it's not simple thing.
i grab ext 3 different tables myl_u, mya_u , myc_c l, a , c type of users.
the l *a* , c tables queried like
select l_id, ext, name, .. myl_u select a_id, ext, name, .. myl_a select c_id, ext, name, .. myc_c
in inbox table, sender_id corresponds l_id, a_id , c_id
you can join tables on sender id , use ifnull
method grab right ext.
select * , ifnull(tl.ext, ifnull(ta.ext, tc.ext)) ext , ifnull(tl.name, ifnull(ta.name, tc.name)) name inbox t1 left join myl_u tl on t1.sender_id=tl.l_id , t1.sender_type='l' left join mya_u ta on t1.sender_id=ta.a_id , t1.sender_type='a' left join myc_c tc on t1.sender_id=tc.c_id , t1.sender_type='c' receiver_id=:receiver_id , receiver_type='a' , spam!='y' order date desc, time desc limit :paging offset :offset
Comments
Post a Comment