You are here: Re: Cross select querying three tables « PHP SQL « IT news, forums, messages
Re: Cross select querying three tables

Posted by Boris Stumm on 06/01/07 09:38

David CZ wrote:
[...]
> SELECT products.id, products.name, tags.id
> FROM products INNER JOIN (tags, product_tag)
> ON (product_tag.product_id = products.id AND product_tag.tag_id =
> tags.id)
> WHERE products.id = 12;

The join with tags is not necessary.


> Which gives me two lines of results:
>
> 12 Product1 1
> 12 Product1 5
>
> I would like a result like this though:
>
> 12 Product1 1,5

Since you do not know how many tags a product has, this is
not possible (at least to my knowledge)

But that is not so much of a problem, you can do that in
your PHP code.

The only way I can think of, but I'd consider it a "dirty"
way, is the following:

* your tags are numbered 1, 2, 4, 8, ... This is possible
if you do not have too many tags, maybe maximum 32 or so
if you use 4-byte integers.

* Then, you can change the select to:

select p.id, p.name, sum(pt.tag_id) as tag_mask
from products p, product_tag pt
where p.id = pt.product_id
group by p.id, p.name

You will then have one tuple per product, with the
tags contained in the tag_mask.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация