Select max using join not returning rows - mysql

I'm stucked in this MYSQL SELECT:
SELECT episode.name
FROM tv.episode
JOIN tv.show ON episode.show_id = show.id
WHERE show.id = 73545
AND season_number = 4
AND episode.number =(SELECT MAX(number) FROM tv.episode WHERE season_number = 4)
Returns 0 rows
The return from the second select is 22, so it works.
Can you point me in the right direction?
Thanks!

You are not including WHERE show.id = 73545 in your subquery with the MAX.

Related

which is the best / efficient way to execute MySQL query

1st approach :-
SELECT
clipComment.commentId,
clipComment.commentedBy,
clipComment.clipId AS commentTypeId,
'clip' AS commentType,
clipComment.commentDescription,
clipComment.commentCreatedDateTime,
clipComment.commentModifiedDateTime,
clipComment.commentLikeCount,
userProfile.userName,
userProfile.firstName,
userProfile.LastName,
userProfile.profilePicUrl,
userProfile.themeForeground,
userProfile.themeBackground,
IF(derCommentLike.commentId = clipComment.commentId,
1,
0) likedByMe
FROM
clipComment
LEFT JOIN
(SELECT
*
FROM
clipCommentLikes
WHERE
commentLikedBy = 16) derCommentLike
ON
derCommentLike.commentId = clipComment.commentId
LEFT JOIN
userProfile
ON
userProfile.userId = clipComment.commentedBy
WHERE
clipComment.clipId = 141
2nd approach :-
SELECT
clipComment.commentId,
clipComment.commentedBy,
clipComment.clipId AS commentTypeId,
'clip' AS commentType,
clipComment.commentDescription,
clipComment.commentCreatedDateTime,
clipComment.commentModifiedDateTime,
clipComment.commentLikeCount,
userProfile.userName,
userProfile.firstName,
userProfile.LastName,
userProfile.profilePicUrl,
userProfile.themeForeground,
userProfile.themeBackground,
IF( derCommentLike.commentId = clipComment.commentId , 1 , 0 ) AS likedByMe
FROM
(SELECT
*
FROM
clipCommentLikes
WHERE
commentLikedBy = 16) derCommentLike
RIGHT OUTER JOIN clipComment
ON derCommentLike.commentId = clipComment.commentId
RIGHT OUTER JOIN userProfile
ON clipComment.commentedBy = userProfile.userId
WHERE
clipComment.clipId = 141
both query returns same result, but just want to know which approach should i follow & which one is more efficient to follow. record set will contain millions of record, so i want to use best way. or i am doing work then please correct me. thank you in advance.
explain statement 1st approach
explain statement 2nd approach
explain statement 1st approach
IF(derCommentLike.commentId = clipComment.commentId, 1, 0) likedByMe
...
LEFT JOIN
(SELECT *
FROM clipCommentLikes
WHERE commentLikedBy = 16
) derCommentLike
ON derCommentLike.commentId = clipComment.commentId
-->
( EXISTS SELECT * FROM clipCommentLikes
WHERE commentId = clipComment.commentId
) AS likedByMe
Explanation:
JOIN ( SELECT ... ) has no index to make it efficient
LEFT JOIN ( ... ) begs for evaluating the subquery after the left table, thereby begging for the subquery to be evaluated repeatedly.
SELECT * (in your subquery) is gathering lots of stuff that is not used. (SELECT * in EXISTS does not mean to fetch everything; the * is just a place holder.)
EXISTS evaluates to 1 (true) or 0 (false), which seems to be what you want.

MySQL select in update statement

This MySQL statement give me all id_duel_player for player with id_player=30 and it work fine.
SELECT b.id_duel_player
FROM duels a
INNER JOIN duel_player b
ON a.id_duel = b.id_duel
WHERE id_player = 30
UNION ALL
SELECT c.id_duel_player
FROM duel_player c
INNER JOIN
(
SELECT aa.*
FROM duels aa
INNER JOIN duel_player bb
ON aa.id_duel = bb.id_duel
WHERE bb.id_player = 30
) d ON c.id_duel = d.id_duel AND c.id_player <> 30
I want to make MySQL statement for UPDATE (fields from duel_player table) all of this id_duel_player that returns this select statement.
UPDATE duel_player
SET num = 2,
total = 5
WHERE (duel_player.id_duel_player = id_duel_player's from above SELECT statement)
I want most effective and fastest way to do this.
Thanks
For 200-400 rows it's likely fastest to create a temporary table with the results, and then do the UPDATE with a join:
CREATE TEMPORARY TABLE id_duel_players AS
SELECT b.id_duel_player as id FROM duels a ...
UPDATE duel_player
JOIN id_duel_players ON duel_player.id_duel_player = id_duel_players.id
SET num = 2,
total = 5
For smaller result sets you may find the IN operator sufficiently fast (... WHERE id_duel_player IN (SELECT ...)), but I've found it unreliable for result sets with hundreds of rows. (Unreliable = suddenly no matches are found, no idea why, I haven't investigated.)

display from database

SELECT * FROM `tbl_wines`
LEFT JOIN `tbl_wines_attrib`
ON `tbl_wines`.`intWinesID` = `tbl_wines_attrib`.`intWinesID`
AND `tbl_wines_attrib`.`intAttributeValueId` = 4
WHERE `intStatus` = 1
LIMIT 0 , 20
I need to know if this query is correct. But am getting all the values from tbl_wines not only the value where intAttributeValueId = 4.
Can anyone help out?
It looks like that condition has been placed as part of your JOIN rather than a WHERE condition. Instead try the following, moving tbl_wines_attrib.intAttributeValueId =4into the WHERE clause.
SELECT * FROM `tbl_wines` LEFT JOIN `tbl_wines_attrib` ON `tbl_wines`.`intWinesID` = `tbl_wines_attrib`.`intWinesID` WHERE `tbl_wines_attrib`.`intAttributeValueId` = 4 AND `intStatus` =1 LIMIT 0 , 20

Mysql Subquery - Need to make a connection between two like queries in order to Count Occurrences of Postal Codes and Zip Codes

I'm having a bit of an issue figuring this out. I have two queries that I need to put together and no matter what I've tried I always end up with an error in mysql syntax. The two queries are below.
Query 1 - this is for canadian postal codes
SELECT ta.city, ta.email_address, ta.country_code, COUNT(*)
FROM jos_form_submitteddata_form1 tb
JOIN jos_postalzip_redirect ta ON UPPER(LEFT(tb.FIELD_24, 3)) = LEFT(ta.postal_zip, 3)
WHERE LENGTH(tb.FIELD_24) > 5
GROUP BY ta.city;
Query 2 - This if for US zip codes
SELECT ta.city, ta.email_address, ta.country_code, COUNT(*)
FROM jos_form_submitteddata_form1 tb
JOIN jos_postalzip_redirect ta ON UPPER(tb.FIELD_24) = LEFT(ta.postal_zip, 5)
WHERE LENGTH(tb.FIELD_24) <= 5
GROUP BY ta.city
HAVING ta.country_code = 'US';
The goal is to have both of these queries display in the same table with correct counts
Try using a UNION statement.
http://dev.mysql.com/doc/refman/5.0/en/union.html
A UNION doesn't do what you need?
SELECT ta.city, ta.email_address, ta.country_code, COUNT(*)
FROM jos_form_submitteddata_form1 tb
JOIN jos_postalzip_redirect ta ON UPPER(LEFT(tb.FIELD_24, 3)) = LEFT(ta.postal_zip, 3)
WHERE LENGTH(tb.FIELD_24) > 5 GROUP BY ta.city
UNION
SELECT ta.city, ta.email_address, ta.country_code, COUNT(*)
FROM jos_form_submitteddata_form1 tb
JOIN jos_postalzip_redirect ta ON UPPER(tb.FIELD_24) = LEFT(ta.postal_zip, 5)
WHERE LENGTH(tb.FIELD_24) <= 5 AND ta.country_code = 'US' GROUP BY ta.city

Mysql num rows on multiple tables using UNION

I'm trying to find number of rows for two tables:
Example 1 returns 81 (which is wrong).
SELECT p_id FROM j_posts
INNER JOIN j_blogs ON p_blog_id = b_id && b_approved = 1
WHERE p_is_draft = 0
UNION SELECT ep_id FROM j_external_posts
INNER JOIN j_blogs ON ep_blog_id = b_id && b_approved = 1
I have then tried to split up the query in two:
SELECT ep_id FROM j_external_posts INNER JOIN j_blogs ON ep_blog_id = b_id
&& b_approved = 1
First part of the query returns 70, and the second returns 39. This is the right amount of rows.
SELECT p_id FROM j_posts INNER JOIN j_blogs ON p_blog_id = b_id
&& b_approved = 1 WHERE p_is_draft = 0
What am I doing wrong in Example 1? It should return 109 instead of 81.
Thanks in advance
Use UNION ALL, UNION would return distinct rows
I think you must say "UNION ALL" instead of UNION.
UNION Does a implicit DISTINCT so same values are displayed only once..