Although I am new to cake, this SQL query could work with the query()
function

SELECT  t1.prod_id
FROM    HABTMtable t1
WHERE   t1.user_id=1
AND     t1.prod_id IN
                (SELECT t2.prod_id
                FROM    HABTMtable t2
                WHERE   t2.user_id=2)

Second SELECT gets all products from user2, while first SELECT  gets
products that belong to user 1 and are in the second query (belonging
to both users)
On 9 jun, 10:40, Céryl <[email protected]> wrote:
> Allright,
> I;ve been cracking my head on this.
>
> I have a HABTM relation between users and products. Users can select a
> product to put on his favorite list. Everything works great (A lot of
> thanks to this Googlegroups for that!) but now I need something heavy.
>
> So I have a users table, products table and the HABTM table with
> user_id and product_id. From this table I can get, using Containable
> behaviour) the products belonging to a certain user.
>
> Now I want to match the lists of two users together. So using two id's
> from two different users, I need to get a list of products from the
> table that occur in both users list.
>
> So:
> User1       User2
> Product1  Product2
> Product3  Product3
> Product5  Product6
> Product6
>
> Needs to return an array containing Product 3 and product 6. I've been
> reading and re-reading containable behaviour and the internet, and
> though about getting both lists from the database and just use a PHP
> query to match. Is there an easy solution, or do you recommend using
> PHP instead of SQL for this, timewise?
>
> Thanks a million!

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to