I am creating a multisite CMS with codeigniter and am having trouble with a query for navigation links. Each page can have meta values that override the defaults (location_id = 0) with ones specific to the site if set. I have two tables cms_pages and cms_page_meta storing the information. Below is the relevant data
Table cms_pages
page_id
Table cms_page_meta
meta_id
location_id
page_id
tag_name
tag_value
I would like to do this in one call if feasible.
This is the call I was using, it causes pages to be listed twice if a title value exists for all locations (location_id = 0) and a specific location (location_id = x) where I just want the specific location if it exists otherwise the generic, otherwise NULL
SELECT cms_page.*, cms_page_meta.tag_value
FROM cms_pages
LEFT JOIN cms_page_meta ON cms_page_meta.page_id = cms_pages.page_id
WHERE cms_page_meta.location_id IN (0, x)
ORDER BY cms_pages.page_order`
I’m pretty sure I should to use coalesce but just can’t figure out how to implement it. Am I trying to do too much at once? Should I split the calls and process with PHP array_merge? I’d like to avoid that as I can cache the call and delete/regenerate the cache whenever I make a change to the pages or metadata. The server gets ~10,000 hits a day so speed is helpful but not necessary yet…
I changed the above query so it only grabs generic data for right now. Any help is greatly appreciated!
I think you should use something like this: