MYSQL query table joins gives duplicate rows - mysql

I have the tables movie, movie_client, language_movie, language, subtitle_movie and subtitle
I want to select all movies with their subtitle and language where client_id (in movie_client) is 1
SELECT * FROM movie
LEFT OUTER JOIN movie_client
ON movie.movie_id = movie_client.movie_client_id
LEFT OUTER JOIN client
ON movie_client.client_movie_id = client.client_id
LEFT OUTER JOIN language_movie
ON movie.movie_id = language_movie.movie_id
LEFT OUTER JOIN language
ON language_movie.language_id = language.language_id
LEFT OUTER JOIN subtitle_movie
ON movie.movie_id = subtitle_movie.movie_id
LEFT OUTER JOIN subtitle
ON subtitle_movie.subtitle_id = subtitle.subtitle_id
WHERE client.client_id=1
this does nog work cause i get duplicate rows i tried inner joins as swell but it just wont work. Can anyone help me with the right query?

Give it a try :
SELECT distinct m.movie_id, lm.language_id, sm.subtitle_id FROM movie m
INNER JOIN movie_client mc
ON m.movie_id = mc.movie_client_id
INNER JOIN client
ON mc.client_movie_id = c.client_id
INNER JOIN lm
ON m.movie_id = lm.movie_id
INNER JOIN language l
ON lm.language_id = l.language_id
INNER JOIN subtitle_movie sm
ON m.movie_id = sm.movie_id
INNER JOIN JOIN subtitle s
ON sm.subtitle_id = s.subtitle_id
WHERE client.client_id=1

This
FROM movie
LEFT OUTER JOIN movie_client
ON movie.movie_id = movie_client.movie_client_id
LEFT OUTER JOIN client
ON movie_client.client_movie_id = client.client_id
should be INNER JOINs you don't need all movies
SELECT * FROM movie
INNER JOIN movie_client
ON movie.movie_id = movie_client.movie_client_id
INNER JOIN client
ON movie_client.client_movie_id = client.client_id
LEFT OUTER JOIN language_movie
ON movie.movie_id = language_movie.movie_id
LEFT OUTER JOIN language
ON language_movie.language_id = language.language_id
LEFT OUTER JOIN subtitle_movie
ON movie.movie_id = subtitle_movie.movie_id
LEFT OUTER JOIN subtitle
ON subtitle_movie.subtitle_id = subtitle.subtitle_id
WHERE client.client_id=1
Check this to understand difference INNER and OUTER JOIN

Related

MySQL LEFT OUTER JOIN not giving correct value

I have the following tables and relationships
Here are the sample data -
I tried the following query-
SELECT project.*, SUM(schedule.amount) as sch_total, SUM(expense.amount) as expense_total, SUM(bill.amount) as bill_total from project
LEFT OUTER JOIN schedule on project.project_id = schedule.project_id
LEFT OUTER JOIN site on project.project_id = site.project_id
LEFT OUTER JOIN expense on site.site_id = expense.site_id
LEFT OUTER JOIN bill on site.site_id = bill.site_id
GROUP BY project.project_id
This is the query result -
But "bill_total" should be 200, not 400.
What did I miss in the query?
Your expense table have multiple rows for same Id. That table must be aggregated first and then used in join -
SELECT project.*,
SUM(schedule.amount) as sch_total,
SUM(E.amount) as expense_total,
SUM(bill.amount) as bill_total
FROM project
LEFT OUTER JOIN schedule on project.project_id = schedule.project_id
LEFT OUTER JOIN site on project.project_id = site.project_id
LEFT OUTER JOIN (SELECT site_id, SUM(expense.amount)amount
FROM expense
GROUP BY site_id) E on site.site_id = expense.site_id
LEFT OUTER JOIN bill on site.site_id = bill.site_id
GROUP BY project.project_id

Left Outer Join Subquery

I am trying to do a left outer join to a subquery, is that possible?
Can I do something like this?:
##this is this weeks targets
select * from targets t
inner join streams s on s.id = t.stream_id
where t.week_no =WEEKOFYEAR(NOW())
left outer join
(
###############This is records selected so far this week
select p.brand_id, p.part_product_family, sum(r.best) from records r
inner join products p on p.id = r.product_id
left outer join streams s on s.body = p.brand_id and s.stream = p.part_product_family
where WEEKOFYEAR(r.date_selected) =WEEKOFYEAR(NOW())
group by p.brand_id, p.part_product_family;
) sq_2
on s.stream = sq_2.part_product_family
This is working:
##this is this weeks targets
select * from targets t
inner join streams s on s.id = t.stream_id
left outer join
(
###############This is records selected so far this week
select p.brand_id, p.part_product_family, sum(r.best) from records r
inner join products p on p.id = r.product_id
left outer join streams s on s.body = p.brand_id and s.stream = p.part_product_family
where WEEKOFYEAR(r.date_selected) =WEEKOFYEAR(NOW()) and YEAR(r.date_selected) = YEAR(now())
group by p.brand_id, p.part_product_family
) sq_2
on s.body = sq_2.brand_id and s.stream = sq_2.part_product_family

Match Against Left Outer Join

SELECT p.product_id,p.account_id,i.image_id,a.email,p.title,p.price
FROM products AS p
LEFT OUTER JOIN products_images AS i
ON p.product_id = i.product_id AND i.featured=1 AND i.deleted=0
INNER JOIN accounts AS a
ON p.account_id = a.account_id
MATCH(p.title) AGAINST('+images')
I'm trying to use a MATCH for the first time. It says that I have a syntax error and I am not sure why?
You're missing the WHERE keyword before conditions that aren't part of the join:
SELECT p.product_id,p.account_id,i.image_id,a.email,p.title,p.price
FROM products AS p
LEFT OUTER JOIN products_images AS i
ON p.product_id = i.product_id AND i.featured=1 AND i.deleted=0
INNER JOIN accounts AS a
ON p.account_id = a.account_id
WHERE MATCH(p.title) AGAINST('+images')

SQL query with deeper relationship

Table: user
Columns
- id
- username
- full_name
Table: pet
Columns
- id
- pet_name
- color_id
Table: pet_color
Columns
- id
- color
Table: results
Columns
- id
- user_id_1
- user_id_2
- user_id_3
- pet_id
- date
- some_text
This:
SELECT A.id, B.full_name, C.full_name, D.full_name, E.pet_name, A.date, A.some_text
FROM RESULTS AS A
LEFT OUTER JOIN USER AS B ON A.USER_ID1 = B.ID
LEFT OUTER JOIN USER AS C ON A.USER_ID2 = C.ID
LEFT OUTER JOIN USER AS D ON A.USER_ID3 = D.ID
LEFT OUTER JOIN PET AS E ON A.PET_ID = E.ID
will give me almost everything that I want except 'pet_color.color', but I can not figure it out what should I add to the query to get that too.
SELECT A.id, B.full_name, C.full_name, D.full_name, E.pet_name, A.date, A.some_text, pc.color
FROM RESULTS AS A
LEFT OUTER JOIN USER AS B ON A.USER_ID1 = B.ID
LEFT OUTER JOIN USER AS C ON A.USER_ID2 = C.ID
LEFT OUTER JOIN USER AS D ON A.USER_ID3 = D.ID
LEFT OUTER JOIN PET AS E ON A.PET_ID = E.ID
LEFT OUTER JOIN PET_COLOR PC on E.color_id = pc.id
SELECT A.id, B.full_name, C.full_name, D.full_name, E.pet_name, A.date, A.some_text, PC.color
FROM RESULTS AS A
LEFT OUTER JOIN USER AS B ON A.USER_ID1 = B.ID
LEFT OUTER JOIN USER AS C ON A.USER_ID2 = C.ID
LEFT OUTER JOIN USER AS D ON A.USER_ID3 = D.ID
LEFT OUTER JOIN PET AS E ON A.PET_ID = E.ID
LEFT OUTER JOIN PET_COLOR PC ON E.COLOR_ID = PC.ID
Not tried, but this should work
SELECT A.id, B.full_name, C.full_name, D.full_name, E.pet_name, A.date, A.some_text
FROM RESULTS AS A
LEFT OUTER JOIN USER AS B ON A.USER_ID1 = B.ID
LEFT OUTER JOIN USER AS C ON A.USER_ID2 = C.ID
LEFT OUTER JOIN USER AS D ON A.USER_ID3 = D.ID
LEFT OUTER JOIN PET AS E ON A.PET_ID = E.ID
INNER JOIN PET_COLOR PC ON PC.ID = E.COLOR_ID

Left outer join in access?

Following query does not work in access.
SELECT Fields.FieldId, PrecisionSettings.DecimalPlaces
from Fields left outer join FieldGroup on Fields.FieldGroupId = FieldGroup.FieldGroupId
left outer join Category on FieldGroup.CategoryId = Category.CategoryId
left outer join PrecisionSettings on
Category.InputAttributesID=PrecisionSettings.AttributesID
It gives error as missing operator in query expression.
In Access you can only join two results at a time. To join more tables you need more parentheses:
SELECT
Fields.FieldId,
PrecisionSettings.DecimalPlaces
FROM
(
(
Fields
LEFT OUTER JOIN FieldGroup ON Fields.FieldGroupId = FieldGroup.FieldGroupId
)
LEFT OUTER JOIN Category ON FieldGroup.CategoryId = Category.CategoryId
)
LEFT OUTER JOIN PrecisionSettings ON Category.InputAttributesID = PrecisionSettings.AttributesID