Match Against Left Outer Join - mysql

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

Related

SQL LIKE seems not work like i think it should

I have an SELECT statement that has a huge number of left join and I want to filter some out.
When I check how many records i have in total and subtract the records with my LIKE statements, I should get the amount that is not affected by my restrictions.
But when I negate my restriction to get the ones I didn't affect, I get an different number than calculated.
SQL without restrictions (Record count: 13.251.981)
SELECT p.product_number
FROM product p
LEFT JOIN product_category pc on p.id = pc.product_id
LEFT JOIN product_category_tree pct on p.id = pct.product_id
LEFT JOIN product_configurator_setting pcs on p.id = pcs.product_id
LEFT JOIN product_cross_selling pcs2 on p.id = pcs2.product_id
LEFT JOIN product_cross_selling_assigned_products pcsap on p.id = pcsap.product_id
LEFT JOIN product_cross_selling_translation pcst on pcs2.id = pcst.product_cross_selling_id
LEFT JOIN product_custom_field_set pcfs on p.id = pcfs.product_id
LEFT JOIN product_media pm on p.id = pm.product_id
LEFT JOIN product_option po on p.id = po.product_id
LEFT JOIN product_price pp on p.id = pp.product_id
LEFT JOIN product_property pp2 on p.id = pp2.product_id
LEFT JOIN product_review pr on p.id = pr.product_id
LEFT JOIN product_search_keyword psk on p.id = psk.product_id
LEFT JOIN product_tag pt on p.id = pt.product_id
LEFT JOIN product_translation pt2 on p.id = pt2.product_id
LEFT JOIN product_visibility pv on p.id = pv.product_id
With restriction (Record count: 9.285.545)
WHERE p.product_number NOT LIKE 'SW%'
AND p.product_number NOT LIKE '%.%'
AND pt2.name NOT LIKE '%Gutschein'
AND pt2.name NOT LIKE '%Test%'
With negated restriction (Record count: 100.851)
WHERE p.product_number LIKE 'SW%'
OR p.product_number LIKE '%.%'
OR pt2.name LIKE '%Gutschein'
OR pt2.name LIKE '%Test%';
From my calculations i should get 3.966.436 records that don't get affected. (13.251.981 - 9.285.545 = 3.966.436)
But instead I get 100.851
How is that possible?
The solution for me was actually this WHERE:
WHERE p.product_number < 'SW'

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

MYSQL query table joins gives duplicate rows

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

Correct Join Query for SQL Query

I currently have the following Query
SELECT * FROM tbl_Cars c
INNER JOIN tbl_Car_Parts cp ON c.Id = cp.Id
INNER JOIN tbl_Car_Factory cf ON cp.Id = cf.Id
However I have now realised that there are some Ids which are in the Cars Table which are then not in the Car Parts table so these are being omitted from my results while I want them included. I tried changing my INNER JOIN to a LEFT OUTER JOIN but it made no difference in the result set I am returned?
Use LEFT OUTER JOIN in both of the joins.
SELECT * FROM tbl_Cars c
LEFT OUTER JOIN tbl_Car_Parts cp ON c.Id = cp.Id
LEFT OUTER JOIN tbl_Car_Factory cf ON cp.Id = cf.Id
Otherwise the second INNER JOIN will invalidate your first LEFT OUTER JOIN for those records that does not have ID (does not join) in the tbl_Car_Parts table.
After a LEFT OUTER JOIN you may be only use again INNER JOIN if the table you are joining is not related with the previous ones that are joined using the LEFT OUTER JOIN.
LEFT JOIN should have definitely solved your problem, Not sure why it didnt work.
Just to be safe I used Left join on both the tables...
SELECT * FROM tbl_Cars c
LEFT JOIN tbl_Car_Parts cp ON c.Id = cp.Id
LEFT JOIN tbl_Car_Factory cf ON cp.Id = cf.Id

Mysql query help. I need to make sure atleast one record is returning

I have a query that I know it should atleast return one row. How can i modify my follow query to make sure that album returns data. thanks for any help.
Query Here. I know that Album has a row and I need to return it.
select distinct p.*,
a.ID as parentalbumid,a.CreatorID as albumcreatorid,a.AlbumName,a.AlbumDescription,a.AlbumDefaultImageURL,a.Private,a.DateCreated,a.AdultContent,a.PasswordProtected,a.AllowTags,a.TypeID,a.AlbumAutoID,
mainuser.Username as mainuserusername,mainuser.ID as mainuserid,mainuser.PictureUrl as mainuserpictureurl,
c.ID as commentID,c.PhotoID as commentphotoid,c.OutputMessage,c.CommentDate,
t.ID as tagID,t.PhotoID as tagphotoid,t.UserID,t.TextTag,t.LeftLocation,t.TopLocation,
u.ID as userid,u.Username,u.FirstName,u.LastName,u.PictureUrl
from photos p
inner join albums a on a.ID = p.AlbumID
inner join users mainuser on mainuser.ID = p.UserID
left join comments c on c.PhotoID = p.ID
left join tags t on t.PhotoID = p.ID
left join users u on u.ID = c.CommentBy
where a.AlbumAutoID = 3
order by p.DateUploaded desc;
Use only LEFT JOINs (and put the table albums as the first table of your FROM):
select distinct p.*,
a.ID as parentalbumid,a.CreatorID as albumcreatorid,a.AlbumName,a.AlbumDescription,a.AlbumDefaultImageURL,a.Private,a.DateCreated,a.AdultContent,a.PasswordProtected,a.AllowTags,a.TypeID,a.AlbumAutoID,
mainuser.Username as mainuserusername,mainuser.ID as mainuserid,mainuser.PictureUrl as mainuserpictureurl,
c.ID as commentID,c.PhotoID as commentphotoid,c.OutputMessage,c.CommentDate,
t.ID as tagID,t.PhotoID as tagphotoid,t.UserID,t.TextTag,t.LeftLocation,t.TopLocation,
u.ID as userid,u.Username,u.FirstName,u.LastName,u.PictureUrl
from albums a
left join photos p on a.ID = p.AlbumID
left join users mainuser on mainuser.ID = p.UserID
left join comments c on c.PhotoID = p.ID
left join tags t on t.PhotoID = p.ID
left join users u on u.ID = c.CommentBy
where a.AlbumAutoID = 3
order by p.DateUploaded desc;