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

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.

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);

SQL Join involving 3 tables, how to?

SQL newbie here.
So we have 3 tables:
categories(cat_id,name);
products(prod_id,name);
relationships(prod_id,cat_id);
It is a one-to-many relationship.
So, given a category name say "Books". How do I find all the products that come under books?
As an example,
categories(1,Books);
categories(2,Phones);
products(302,Sherlock Holmes);
relationships(302,1);
You need to JOIN the three tables.
SELECT p.*
FROM relationships r
INNER JOIN products p
ON p.prod_id = r.prod_id
INNER JOIN categories c
ON c.cat_d = r.cat_id
WHERE c.name = 'Books'
You have to join tables on related columns and specify WHERE clause to select all records where category name = 'Books'
SELECT p.*
FROM categories c
JOIN relationships r ON c.cat_id = r.cat_id
JOIN products p ON r.prod_id = p.prod_id
WHERE c.name = 'Books' -- or specify parameter like #Books
In SQL you often join related tables and beginners tend to join, whatever the situation. I would not recommend this. In your case you want to select products. If you only want to show products data, select from products only. You want to select products that are in the category 'Books' (or for which exists an entry in category 'Books'). Hence use an IN or EXISTS clause in order to find them:
select * from products
where prod_id in
(
select prod_id
from relationships
where cat_id = (select cat_id from categories where name = 'Books')
);
Thus you get a well structured query that tells the reader easily how the tables are related and what data you are actually interested in. Later, with different tables and data to select, this may keep you from duplicate result rows that you must get rid of by using DISTINCT or from getting wrong aggregates (sums, counts, etc.), because of mistakenly considering records multifold.
try this:
select p.Prod_id,p.name
from products p inner join relationships r on
p.prod_id = r.prod_id
where r.cat_id = (select cat_id from categories where name = 'books')
or
select p.Prod_id,p.name
from products p inner join relationships r on
p.prod_id = r.prod_id inner join categories c on c.cat_id = r.cat_id
where c.name = 'books'

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

MYSQL query through many layers of one to many relationships

How do I select all things from the many of a one to many relationship, several layers down in many one-to-many relationships? I'm not sure how to nest all those left joins.
Example
There are many Institutions
Each Institution has many Departments
Each Department has many Forums
Each Forum has many Users
Each User has many Posts
Each Post has many Comments
Find all Comments for an Institution.
I googled around, but I'm not sure what the name for this is - chain nested one to many relationship queries? The closest I found was SQL left join vs multiple tables on FROM line?
Without knowing your schema and making some assumptions:
SELECT c.*
FROM
Institution i
INNER JOIN Department d ON d.InstitutionID = i.InstitutionID
INNER JOIN Forums f ON f.DepartmentID = d.DepartmentID
INNER JOIN `User` u ON u.ForumID = f.ForumID
INNER JOIN Post p ON p.UserID = u.UserID
INNER JOIN Comment c ON c.PostID = p.PostID
WHERE
i.InstitutionID = 42

How to best do a SQL left join null check where all must be null?

Suppose I have table Person, who owns many Books, which cover many Subjects.
A Book might not yet have any Subjects assigned to them.
It's easy to query for Persons who own SOME Books that have no Subjects with a left join and a null check. e.g.:
select * from persons p
inner join person_book pb on pb.person_id = p.id
left join book_subject bs on bs.book_id = person_book.book_id
where book_subject.book_id is null;
What is the best way to query for a Person who ONLY owns books with no established subjects?
I know I can find people with books, and people with at least 1 book with subjects, then find the difference. But is there a direct way?
(This would be in MySQL if it makes a difference).
Try:
select p.*
from persons p
inner join person_book pb on pb.person_id = p.id
left join book_subject bs on bs.book_id = person_book.book_id
group by p.id
having count(bs.book_id) = 0