SELECT distinct main.id, etc. etc.

At 14:37 11-3-04, you wrote:
I have 3 tables:

main(id int, type tinyint(1))
categories(id int, name varchar)
items(id int, name varchar)

I want to select the id and name.
If type is 1 then I want to select the name from categories,
if type is 0 I want to select the name from items, here is the query I'm
trying to use:

SELECT main.id, IF(main.type,categories.name,items.name),
IF(main.type,"cat","item") AS type FROM main,items,categories WHERE
IF(main.type,categories.id,items.id)=main.id;

This query gives me each row couple of times, can anyone tell me why? or can
any one give me a better solution?

My solution which I guess is not good is adding GROUP BY:

SELECT main.id, IF(main.type,categories.name,items.name),
IF(main.type,"cat","item") AS type FROM main,items,categories WHERE
IF(main.type,categories.id,items.id)=main.id GROUP BY id, type;

Thanks,
        -Amir.


-- 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