Retrieve Clients from Projects User Belongs to - mysql

Users can have multiple projects and projects can have multiple clients.
How do I get all the unique clients from the projects a user belongs to?
Tables:
- Users
- Projects
- Clients
- Project Clients
SELECT client_id, client_name FROM clients.. ? JOINS, USING.. ?.. what?

Firstly, if users can have multiple projects, you'll need a Project-Users table.
Given that, you can get what you want using either of these pieces of SQL:
select distinct c.id from
clients c
join projectclients pc on c.id=pc.clientid
join projects p on pc.projectid=p.id
join projectusers pu on p.id=pu.projectid
join users u on u.id=pu.userid
where u.id=3
select distinct c.id from
clients c join projectclients pc on c.id=pc.clientid
where pc.projectid in
(select projectid from
users u join projectusers pu on u.id=pu.userid
where u.id=3)

Related

to get different columns from single query

Consider the data about projects and members stored in tables below.

Projects(id, title)
Members(id, name, project_id)

Generate a report to list all the projects, members working on them. Also list the projects that do not have any employees assigned yet and also the employees who are available as free resources.
Table-
Projects(id, title)
Members(id, name, project_id)
can someone optimize the query as i applied outer join and i got all answers together.i need to separate the columns as per required question.
Select p.title, m.id
From projects p FULL OUTER JOIN
Members m
where p.id = m.project_id;
MySQL does not support full join. So do this in two steps:
Select p.title, m.id
From projects p LEFT JOIN
Members m
ON p.id = m.project_id
union all
select null, m.id
from members m
where not exists (select 1 from projects p where p.id = m.project_id);

Redmine: What is the sql relationship between projects and contacts (companies)

I have a hard time figuring out the SQL relationship between the companies from the contacts table (where is_company=1) and projects.
The following query works for 95% of the time, but I came to the conclusion that the contacts.first_name is not the relationship to the projects table:
SELECT p.id, p.name FROM custom_values
INNER JOIN custom_fields c ON c.id = custom_values.custom_field_id
INNER JOIN projects p ON p.id = customized_id
WHERE custom_values.value LIKE '%{CONTACT_FIRST_NAME}%'
Please help.

how do i merge 3 select statements from different tables into 1 specific query?

i can write the specific query for that table and its relation, but merging them into one is the difficulty i'm having with
select clients, count(*)
from clients
inner join themes
on clients.id = themes.client_id
group by clients
select themes.id
from themes
inner join videos
on videos.theme_id = themes.id
select distinct videos.user_id,
from videos
inner join properties
on properties.user_id = videos.user_id
group by properties.user_id
basically, i want to count the amount of unique users for a client
the relationship is
a client has many themes
a theme has many videos
a video has one property
a user has many properties
thanks
Count the amount of unique users for a client:
select clients, count(DISTINCT properties.user_id) as num_users
from clients
inner join themes on clients.id = themes.client_id
inner join videos on videos.theme_id = themes.id
inner join properties on properties.user_id = videos.user_id
group by clients.clients_id;
You also might be able to get away with a bit shorter query:
Users always have properties (that is the assumption), then users with a video will be present in the property table and that then does not have to be joined:
select clients, count(DISTINCT videos.user_id) as num_users
from clients
inner join themes on clients.id = themes.client_id
inner join videos on videos.theme_id = themes.id
group by clients.clients_id;

SQL Many to many to many

I'm having an issue with a query I have to build. There are 3 tables,all many to many after one another.
Table Stores - id,store_name
Table Clients - id,store_id,client_name
Table Products - id,client_id,product_name
In a few words - One Product can be bought from many Clients. And one Client can be in many Stores.
The task is to get all Stores with the number of their Clients (a person is a client who bought at least one product. If that client_id does not bought at least 1 Product - he is not a real Client).
SELECT store_name, COUNT(store_name)
FROM Stores s
INNER JOIN Clients c on (s.id = c.store_id )
INNER JOIN Products p on (c.id = p.client_id)
GROUP BY store_name
get a count of clients and then do a left join among the tables like below
select s.store_name,
t.client_count
from stores s left join
(
select c.store_id, count(p.id) as client_count
from Clients c left join products p
on c.id = p.client_id
group by p.client_id
) t on s.id = t.store_id

table with multiple foreign keys joining 1 foriegn key in mysql

Hi I have a database table with the following information :
owner.primaryitowner, (bobsmith#mail.com)
owner.secondaryitowner,
owner.primarybusinessowner,
owner.secondarybusinessowner
users.username (email bobsmith#mail.com)
users.displayname (e.g. Bob Smith)
The issue, is the owners are only stored as emails. There is another table I normally
inner join users on users.username = owner.primaryitowner to get users.displayname
so the data reads correctly.`
I am able to do this
select u.displayname
from users u
inner join owners o on
o.primaryitowner = u.username
or o.secondaryitowner = u.username
or o.primarybusinessowner = u.username
or o.secondarybusinessowner = u.username
The issue is I need to have unique columns not all into one column.
PS I cannot change the database I am only a report writer.
Thanks so much in advance
You will want to join each column of email from users into owners
SELECT u.displayname AS userName
, po.displayName AS PrimaryItOwnerUsernName
, so.displayName AS SecondaryIdOwnerUserName
FROM users AS u
INNER JOIN owners AS po on u.primaryitowner = po.username
INNER JOIN owners AS so ON u.secondaryitowner = so.username
...
WHERE u.UserName = 'Ryan J Morse'
When you join into the owners table (aliased) multiple times, this allows you to change the emails stored in users into the display names you will need for your report.
Does this work for your needs? When you come at it from the point of view of the owner table, it's easier to grab all of the displayname's as separate columns for a single owner record. Going the other way will pull information from muliple owner records for the same user. If that's what you want, then this won't work.
select prim_o.displayname as "Primary IT Owner",
sec_o.displayname as "Secondary IT Owner",
prim_bo.displayname as "Primary Business Owner",
sec_bo.displayname as "Secondary Business Owner"
from owner o
inner join users prim_o (o.primaryitowner = prim_o.username)
inner join users sec_o (o.secondaryitowner = sec_o.username)
inner join users prim_bo (o.primarybusinessowner = prim_bo.username)
inner join users sec_bo (o.secondarybusinessowner = sec_bo.username)