Michael Stassen wrote:
Sebastian wrote:
Michael Stassen wrote:
Sebastian wrote:
i have a query with 3 union selects:
SELECT id, 'news' AS type, .... FROM news
UNION
SELECT id, 'faq' AS type, .... FROM faq
UNION
SELECT id, 'forum' AS type, .... FROM forum
which works just fine and selects everything from all 3 tables..
but say i want to make a condition to only select from either 'faq'
, 'news' or 'forum' how can i do this?
example, if a user visits a link suck as: page.php?cat=faq it will
only select from 'faq' .. is this possible to do right in the
query? when there is no ?cat= then all three selects run.
makes sense? i am stuck on this for a few days already.
thanks.
Why don't you do this in your app? If cat is set, issue the
appropriate single-table query, otherwise issue the union. Surely
that would be simpler than trying to build one multi-purpose query.
Michael
I was hoping i could do some crazy thing like WHERE type = 'faq' so i
can do it all from one block of code.
Frankly, I don't see the benefit of this. What is the payoff? php
was designed for this sort of conditional execution, sql wasn't.
You've spent several days trying to find a way to do this in sql. How
long would it have taken you to write the if...else... statement in
php? Let's see:
if ($cat == 'news' or $cat == 'faq' or $cat == 'forum')
{
$sql = "SELECT id, '$cat' AS type, ... FROM $cat";
}
else
{
$sql = " SELECT id, 'news' AS type, .... FROM news
UNION
SELECT id, 'faq' AS type, .... FROM faq
UNION
SELECT id, 'forum' AS type, .... FROM forum";
}
So, why spend days trying to come up with complicated, ugly sql when
it can be done in 5 minutes with simple, easy-to-read php?
Michael
i only posted snip of the query.. it is much larger and complex than
just a SELECT. i am going MATCH, AGAINST, LEFT JOINS, and other
operations for a search engine. doing it in the app (php in my case)
would mean i would end up with a huge block of SQL code which would seem
more ugly IMO.. on top of that i am paginating the results which would
mean i would have to duplicate the code you posted above since there
would be no point in selecting many other columns just to get a row
count for a the pagination part so i couldn't re-use the IF statement
output as shown above.
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/[EMAIL PROTECTED]