MySQL queries, selecting field from one of many databases -
i have remarks
table can linked number of other items in system, in case of example we'll use bookings
, enquiries
, referrals
.
thus in remarks
table have columns
remark_id | datetime | text | booking_id | enquiry_id | referral_id 1 | 2014-06-28 | abc | 0 | 8 | 0 2 | 2014-06-27 | def | 3 | 0 | 0 2 | 2014-05-31 | ghi | 0 | 0 | 10
etc...
each of item tables have field called name
. when want select remark likelihood i'll need name.
i'd achieve single query, getting 2d array follows:
['remark_id'=>1, 'datetime'=>'2014-06-28', 'text'=>'abc', 'name'=>'harold']
however query i'd expect use be
select r.remark_id,r.datetime,r.text ,b.name book,rr.name referral,e.name enquiry remarks r left join bookings b on b.book_id=r.book_id left join referrals rr on rr.referral_id=r.referral_id left join enquiries e on e.enquiry_id=r.enquiry_id
leaving me output
['remark_id'=>1, 'datetime'=>'2014-06-28', 'text'=>'abc', 'book'=>'harold', 'referral'=>'', 'enquiry'=>'']
and more processing before or during rendering view.
is there way write query such fill field first not null
string encountered in 1 of joined tables?
please suggest using different database system if know mysql
doesn't provide way i'm asking. if it's case can't done there's no business sense in rewriting system anyway, i'd ask!
one way achieve nested if statements:
if(b.name not null, b.name, if(rr.name not null, rr.name, e.name)) name
one drawback gives implicit priority books? not sure if issue.
perhaps main drawback, though, kind of "magical" , has goofy syntax might more clear handle cases in controller after all.
Comments
Post a Comment