Re: [PHP-DB] IF Clause in Left Join

[email protected] ("chris smith")
Newsgroups php.db
Message-ID <[email protected]>
> 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/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.