Re: IF Clause in Left Join
| From: | chris smith | Date: | Mon, 29 Dec 2008 10:49:50 +0000 |
| Subject: | Re: IF Clause in Left Join | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-45841@lists.php.net to get a copy of this message | ||
> I really need some help. I'm trying to use an IF statement in a multi-table
> left join. I'm joining four tables (customers, contents, courses &
> locations).
IF can only be used in the select part or the where part, it can't be
used in the join part of a query.
> In the contents table I have two fields, product_id and value_id that relate
> to different values in different tables depending upon the value in the
> product_type field. If product_type='course' then the product_id and
> value_id relate to the courses and locations table, but if
> product_type='other' then the product_id and value_id relate to the product
> and prices table.
>
> I have two options:
>
> 1. Match only the courses and locations where product_type='course' and
> NULL values if relating to the product and prices table.
> 2. Or change the query (and fields) if the product_type='other' to use the
> product and prices table.
You can union the results.
select ... where product_type='course'
union
select ... where product_type='other'
> I'm using Mysql 4.3.
Didn't know there was such a version ;)
--
Postgresql & php tutorials
http://www.designmagick.com/