I am guessing this is a pretty basic question...

There must be a better way for me to do this, but I'm still too new at this
to know what it is.  I've gone through some JOIN tutorials and the
documentation, but I don't think I'm quite getting it.

(Using MySQL v3.23.38) I have three tables: my_clients, my_users,
my_projects

TABLE my_company:
+---------------------------+
| company_id | company_name |
+---------------------------+
|      1     |   SuperCo    |
|      2     |   Mega Inc.  |
+---------------------------+

TABLE my_users:
+----------------------------------+
| user_id | company_id | user_name |
+----------------------------------+
|    1    |     1      |   Bob     |
|    2    |     1      |   Jane    |
|    3    |     2      |   Steve   |
+----------------------------------+

TABLE my_projects:
+--------------------------------------------------+
| project_id | user_id | company_id | project_name |
+--------------------------------------------------+
|      1     |    1    |      1     | Put on socks |
|      2     |    2    |      1     | Eat lunch    |
|      3     |    3    |      2     | Sing songs   |
+--------------------------------------------------+

I know the company_id and I want to see a list of all projects for that
company.  I can only image that there should be a way that I can make this
work without having to create a company_id column in my_projects, but I
can't get the query to work.

I would like to just get a list of all users with company_id=1 and then show
all projects for that company_id, in the end returning a list of
project_name/user_name.  I can get rid of the company_id if I'm performing a
full join, but then I wind up with duplicates of the project_name
(Project1/User1, Project1/User2, ...).

Here is the query I am using with the tables as they are:

SELECT my_projects.project_name, my_users.user_name FROM my_projects LEFT
JOIN my_users ON (my_projects.user_id=my_users.user_id) WHERE
my_projects.company_id = '$company_id' ORDER BY my_projects.user_id;

This seems to give me a list of unique project names with the associated
user's name.

But if anyone has any corrections/pointers, they would be most appreciated.

Thanks for any help.

Wesley



---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to