views:

170

answers:

1

Ok, I have an app as described in this post: http://stackoverflow.com/questions/1623105/good-database-structure-for-a-new-web-app

I've prepared a scenario to make my question as clear as possible:

Users table:
    +----------+-----------------+
    | user_id  | email           |
    +----------+-----------------+
    | user_1   | [email protected] |
    | user_2   | [email protected] |
    | user_3   | [email protected] |
    | user_4   | [email protected] |
    +----------+-----------------+

*Notice user_3 and user_4 do not have their own domains

Domains table:
    +-----------+------------+
    | owner_id  | domain     |
    +-----------+------------+
    | user_1    | dom-1.com  | 
    | user_1    | dom-2.com  |
    | user_2    | dom-3.com  |
    +------------------------+

The table below, shows who is a user of a domain and who the owner is:

User-Domain table:
    +----------+-------------+-----------+
    | user_id  | domain      | owner_id  |
    +------------------------+-----------+
    | user_1   | dom-1.com   | user_1    |
    | user_1   | dom-2.com   | user_1    |
    | user_2   | dom-3.com   | user_2    |
    | user_3   | dom-1.com   | user_1    |
    | user_3   | dom-2.com   | user_1    |
    | user_4   | dom-1.com   | user_1    |
    +------------------------+-----------+

My question is, how do I output a list of each user who is using a given user's domain, in the following format?

For example (the following is an HTML table):

List of users who are using User_1's domains (HTML TABLE):
        +-------------------+-----------------------+
        | Email             | domains               |
        +-------------------+-----------------------+
        | [email protected]   | dom-1.com, dom-2.com  |
        | [email protected]   | dom-1.com             |
        +-------------------+-----------------------+
+3  A: 

This query should give you the results. Displaying a HTML table from it should be easy enough.

SELECT
   u.email,
   group_concat(d.domain SEPARATOR ', ')
FROM
   domains d
   JOIN user_domain ud ON d.domain=ud.domain AND d.owner_id=ud.owner_id AND d.owner_id!=ud.user_id
   JOIN users u on ud.user_id=u.user_id
WHERE d.owner_id='user_1'
GROUP BY u.email

You basically select all domains owned by 'user_1', then all user_ids for these domains, and then all users for these user_ids. After that, you group the results by the user's email and concatenate all domain names the user is using.

Lukáš Lalinský
ok, let me give this a try
Chad
Lukas, I tried it and I get an error ...#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ANS d.owner_id=ud.owner_id LEFT JOIN users u on ud.user_id=u.user_id WHERE' at line 6
Chad
Well, ANS should be AND :)
Lukáš Lalinský
of course, can I have it NOT return the owner?
Chad
It doesn't return the owner.
Lukáš Lalinský
In fact it does. It seems to return any user who is a user of a domain, and of course each owner is also a user as shown in the User_Domain table.
Chad
Ah, sorry, see my updated answer.
Lukáš Lalinský
I tested the new query and now I get unusual results. It still returns the owner, but this time, the owner's email is NULL and if another user is using a domain, it is not listed for the owner. I don't know if it makes sense but basically this result is worse.
Chad
Well, I could filter out the row where email is NULL but I don't think this is what you expected either.
Chad
Just change LEFT JOIN to JOIN. I initially misunderstood the query and I thought you want a list of domains an all their users, while you wanted a list of users and all their domains.
Lukáš Lalinský
That did it, and that has to be one of the most complex sql queries I've ever used. I've lots to learn. Thanks a bunch Lukas ;)
Chad