MySQL SubSelect, null, and if for section to page condition?
Posted: Sat Mar 21, 2009 10:58 pm
I'm trying to first determine if a section exists; if it does then select a page's information all in a single query.
I think this could probably be done though I'm having difficulty finding decent examples to work from.
So the first thing I have is the following...
If the section exists I'll get it's ID.
What I'd like to do is figure out how to determine if a.id is null; if it is then do nothing (no rows returned) and I can server an HTTP 404.
However if a.id is not null execute another sub-SELECT to get the page's information.
So the second half of the query that I'm trying to figure out how to write is...
if a.id is not null ...and then SELECT the page's contents from the cms_page table.
So let's say as a user you're looking at my example 404 page for whatever reason (the HTTP request would be 200 as you're intentionally looking at the 404 page). Here is an example of some logic that I tried but am unable to find anything even half decent to read from (as examples are what I learn best from).
I think this could probably be done though I'm having difficulty finding decent examples to work from.
So the first thing I have is the following...
Code: Select all
SELECT *FROM ( SELECT id FROM cms_sections WHERE section_url='error' ) AS aWhat I'd like to do is figure out how to determine if a.id is null; if it is then do nothing (no rows returned) and I can server an HTTP 404.
However if a.id is not null execute another sub-SELECT to get the page's information.
So the second half of the query that I'm trying to figure out how to write is...
if a.id is not null ...and then SELECT the page's contents from the cms_page table.
So let's say as a user you're looking at my example 404 page for whatever reason (the HTTP request would be 200 as you're intentionally looking at the 404 page). Here is an example of some logic that I tried but am unable to find anything even half decent to read from (as examples are what I learn best from).
Code: Select all
SELECT *FROM (SELECT id FROM cms_sections WHERE section_url='error') AS aIF a.id IS NOT NULL THEN SELECT * FROM (SELECT * cms_pages FROM cms_pages WHERE id_section=a.id AND url='404.php')