I agree with you,
just see that if ..
sql = "SELECT id, '$cat' AS type, ... FROM $cat";
is exactly what is done in prepare statement.

dynamic sql is better than application level statement preparation, when you use
stored procedure. but since this is the a habit in mysql, i'll keep this for
oracle, and other rdbms.

P.S. haven't spent days to help. It is easy. And since it was a week-end :o)

Mathias

Selon Michael Stassen <[EMAIL PROTECTED]>:

> 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
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]
>
>



-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to