condition LEFT JOIN with DISTINCT - mysql

select wyraz_id, count(wyraz_id) as c from worek w
group by wyraz_id order by c desc limit 11
This query gives me back top 11 favourited wyraz_id from table worek.
LEFT JOIN wyraz FROM wyrazy wy WHERE wy.wyraz_id = w.wyraz_id
The problem is, that wyraz_id in this case should apply to DISTINCT results only.
select wyraz_id, count(wyraz_id) as c from worek w
LEFT JOIN wyraz FROM wyrazy wy
WHERE wy.wyraz_id = w.wyraz_id Having distint(wyraz_id)???
group by wyraz_id order by c desc limit 11
I've managed to go around it by doing another query on the go, but seems pointless really if you can do all that with a simple join.
$d = $row['wyraz_id'];
"select wyraz from wyrazy where wyraz_id='{$d}'";
Thank you kindly for any suggestions.

Related

Order by first results that do not go over

How can you order by two fields based on the value not being greater than another? Here is an example of what I have written.
SELECT *
FROM contest_results r
LEFT
JOIN participant_users u
ON r.participant_id = u.id
LEFT
JOIN contest_entry_tie_answers t
ON u.id = t.participant_id
LEFT
JOIN contest_answers a
ON a.question_id = t.tie_breaker_question_id
WHERE r.contest_id = 20
ORDER BY r.correct_answers DESC,
t.answer <= a.possible_answer ASC
Now imagine that this query returns two rows and contest_entry_tie_answers.answer has a value of 103 and another of 106. Finally, imagine that contest_answers.possible_answer has a value of 105 because that field was checked as the correct answer. I want to be able to order the results as contest_entry_tie_answers.answer <= contest_answers.possible_answer.
That would mean the 103 answer would be shown first in the event that the previous order by contest_results.correct_answers returns a tie value for both. Then the 106 result would show last because it went over the actual correct answer of 105.
Is there a way to do this? I hope this made some sense.
I believe I found the solution by doing the following:
SELECT *
FROM contest_results r
LEFT
JOIN participant_users u
ON r.participant_id = u.id
LEFT
JOIN contest_entry_tie_answers t
ON u.id = t.participant_id
LEFT
JOIN contest_answers a
ON a.question_id = t.tie_breaker_question_id
WHERE r.contest_id = 20
ORDER BY r.correct_answers DESC,
case
when t.answer <= a.possible_answer then 0
when t.answer > a.possible_answer then 1
end,
t.answer
DESC
This seems to work well from my initial testing.

Select only one record from many duplicates

Hello im using the below query to select some elements from the db.
SELECT *,c.slug AS pslug,d.slug AS catslug
FROM uhhu_virtuemart_products as a
LEFT JOIN uhhu_virtuemart_products_el_gr as d
on a.virtuemart_product_id=d.virtuemart_product_id
LEFT JOIN uhhu_virtuemart_product_categories as b
ON a.virtuemart_product_id = b.virtuemart_product_id
LEFT JOIN uhhu_virtuemart_categories_el_gr AS c
ON b.virtuemart_category_id=c.virtuemart_category_id
WHERE a.virtuemart_product_id = 508
The problem is that a product can have many categories , so this query returns me 3 rows, while i need to take always 1 row for output.
virtuemart_product_categories:
What i want is, when it check for the category here :
LEFT JOIN uhhu_virtuemart_product_categories as b
ON a.virtuemart_product_id = b.virtuemart_product_id
put a LIMIT 1 statement
I tried to use a nested select like this:
SELECT *,c.slug AS pslug,d.slug AS catslug
FROM uhhu_virtuemart_products as a
WHERE uhhu_virtuemart_product_id =
(SELECT virtuemart_product_id
from virtuemart_product_categories
where virtuemart_product_id=508 LIMIT 1)
LEFT JOIN uhhu_virtuemart_categories_el_gr AS c
ON a.virtuemart_category_id=c.virtuemart_category_id
LEFT JOIN uhhu_virtuemart_categories_el_gr as d
on c.virtuemart_category_id=d.virtuemart_category_id
And Like this:
SELECT *,c.slug AS pslug,d.slug AS catslug
FROM uhhu_virtuemart_product_categories as a
LEFT JOIN uhhu_virtuemart_products_el_gr as d
on a.virtuemart_product_id=d.virtuemart_product_id
LEFT JOIN uhhu_virtuemart_categories_el_gr AS c
ON a.virtuemart_category_id=c.virtuemart_category_id
WHERE a.virtuemart_product_id =(
SELECT virtuemart_product_id
from uhhu_virtuemart_product_categories as f
where f.virtuemart_product_id=508
LIMIT 1)
I tried also use SELECT distinct but it didnt work too.I know only the basics of SQL and couldnt find something similar to help me solve this, so i would appreciate your help.
Hope i was clear enough.

Duplicate column name SQL - need change alias?

I have written SQL query with a INNER JOIN and Sub-query:
SELECT c.*,
ar.ArticleName,
ar.idArticle,
du.DetailToUsersName,
du.DetailToUsersPhoto,
COUNT(c.idCommentToArticle) AS CNT,
CASE WHEN d.Count IS NULL THEN 0 ELSE d.Count END AS CountLikes
from (select *
from commenttoarticle g
inner join (select distinct(s.idCommentToArticle)
from commenttoarticle s
order by s.CommentToArticlePID limit 3) as gh) as c
LEFT JOIN article ar ON c.CommentToArticleIdArticle = ar.idArticle
LEFT JOIN detailtousers du ON du.idDetailToUsers = c.CommentToArticleIdUser
LEFT JOIN `likes` d ON (d.IdNote = c.idCommentToArticle AND d.LikeType = 6)
WHERE c.CommentToArticleIdArticle = 11
GROUP BY c.idCommentToArticle
ORDER BY c.idCommentToArticle DESC
So, I get error:
Duplicate column name 'idCommentToArticle'
I can not find where the duplication is?
you can specify in the alias table query c
select g.* from commenttoarticle g
instead of
select * from commenttoarticle g
Also you should specify Join condition to limit the rows to 3 as per your intention, with out the ON clause it will be like a cross join.
select g.* from commenttoarticle g
inner join (select distinct(s.idCommentToArticle) from commenttoarticle s order by s.CommentToArticlePID limit 3) as gh
on g.idcommenttoarticle = gh.idcommenttoarticle
As #RADAR has suggested, your inner query joins don't seem to be complete. And I see from comments that once you place the JOIN condition in, then you lose all data. I think this is because neither part of the subqueries were doing what they were supposed to do.
Here is my attempt at a total solution (note, without dataset and table definition I can't show it working). OK, so you have asked the question again over here and provided a SQL-Fiddle, I have updated with a working version, but minus the additional JOIN tables, since they are not defined.
SELECT c.*,
ar.ArticleName,
ar.idArticle,
du.DetailToUsersName,
du.DetailToUsersPhoto,
COUNT(c.idCommentToArticle) AS CNT,
CASE WHEN d.Count IS NULL THEN 0 ELSE d.Count END AS CountLikes
FROM commenttoarticle c -- one layer of subquery not required.
INNER JOIN (select s.idCommentToArticle, s.CommentToArticlePID -- added both the id and the parent id
FROM commenttoarticle s
WHERE s.CommentToArticleIdArticle = 11 -- moved to inner query, instead of outer query
ORDER BY s.idCommentToArticle DESC limit 3) as gh
ON c.idcommenttoarticle = gh.idcommenttoarticle -- add join condition
OR c.idcommenttoarticle = gh.CommentToArticlePID -- which matches id and parent id
LEFT JOIN article ar ON c.CommentToArticleIdArticle = ar.idArticle
LEFT JOIN detailtousers du ON du.idDetailToUsers = c.CommentToArticleIdUser
LEFT JOIN `likes` d ON (d.IdNote = c.idCommentToArticle AND d.LikeType = 6)
GROUP BY c.idCommentToArticle
ORDER BY c.idCommentToArticle DESC
But let me explain a little further, the following code from your original query was selecting the top 3 idCommentToArticlePID,
(select *
from commenttoarticle g
inner join (select distinct(s.idCommentToArticle)
from commenttoarticle s
order by s.CommentToArticlePID limit 3) as gh)
but then because there was no ON specified the 3 records were then joined to every single record from the g reference. This resulted in the full dataset being returned.
And then you you specified WHERE c.CommentToArticleIdArticle = 11 this filtered the result set back down again to something that looked correct.
When you then added the ON (as per #RADAR's suggestion) the inner query did not contain any values that matched the WHERE c.CommentToArticleIdArticle = 11 filter and thus you lost all your results. If you move this filter into the inner query as shown above, then these will work together and not conflict.
Within the JOIN condition, you indicate that you want both the matching articles and their parents, so I added both to the return of the inner query, and checked for either in the join condition.
Also I think the whole g table reference is redundant and can be removed. You should be able to access this table directly as c.
I also have some concerns about the GROUP BY and COUNT (c.idCommentToArticle) - there seem a little strange, but I have no supporting context (ie data examples), so they may be correct. If you still have issues, I would comment the GROUP BY and COUNT statements out, and test to see what data you are getting, before adding these back in.

MySQL Wrong ORDER BY

SELECT `player`.`cid`, `player`.`k`, `player`.`d`, `gg`.`gg_id`, `gg`.`name`, `gg`.`img`, `cc`.`cid`, `cc`.`name`, `cc`.`class`, `cc`.`gg_id`
FROM `player`
LEFT JOIN `cc` ON `cc`.`cid` = `player`.`cid`
LEFT JOIN `gg` ON `gg`.`gg_id` = `cc`.`gg_id`
ORDER BY (`k`-`d`) DESC
i want to order by the K minus the D values, but im not getting it correctly
what im a doing wrong? with or without DESC/ASC, its wrong
Try:
SELECT (player.k-player.d), player.cid, player.k, player.d, gg.gg_id, gg.name, gg.img, cc.cid, cc.name, cc.class, cc.gg_idFROM player LEFT JOIN cc ON cc.cid = player.cid LEFT JOIN gg ON gg.gg_id = cc.gg_id ORDER BY (player.k-player.d) DESC
I did a quick query of my own and the results appear to be unordered (despite the fact the were) until I added the SELECT (player.k-player.d). MySQL also complained about ommiting the table name in the ORDER BY clause.

Need help with an SQL query involving multiple tables - Join not an option

SELECT i.*, i.id IN (
SELECT id
FROM w
WHERE w.status='active') AS wish
FROM i
INNER JOIN r ON i.id=r.id
WHERE r.member_id=1 && r.status='active'
ORDER BY wish DESC
LIMIT 0,50
That's a query that I'm trying to run. It doesn't scale well, and I'm wondering if someone here can tell me where I could improve things. I don't join w to r and i because I need to show rows from i that are unrepresented in w. I tried a left join, but it didn't perform too well. This is better, but not ideal yet. All three tables are very large. All three are indexed on the fields I'm joining and selecting on.
Any comments, pointers, or constructive criticisms would be greatly appreciated.
EDIT Addition:
I should have put this in my original question. It's the EXPLAIN as return from SQLYog.
id|select_type |table|type |possible_keys|key |key_len|ref |rows|Extra|
1 |PRIMARY |r |ref |member_id,id |member_id|3 |const|3120|Using where; Using temporary; Using filesort
1 |PRIMARY |i |eq_ref |id |id |8 |r.id |1 |
2 |DEPENDENT SUBQUERY|w |index_subquery|id,status |id |8 |func |8 |Using where
EDIT le dorfier - more comments ...
I should mention that the key for w is (member_id, id). So each id can exist multiple times in w, and I only want to know if it exists.
WHERE x IN () is identical to an INNER JOIN to a SELECT DISTINCT subquery, and in general, a join to a subquery will typically perform better if the optimizer doesn't turn the IN into a JOIN - which it should:
SELECT i.*
FROM i
INNER JOIN (
SELECT DISTINCT id
FROM w
WHERE w.status = 'active'
) AS wish
ON i.id = wish.id
INNER JOIN r
ON i.id = r.id
WHERE r.member_id = 1 && r.status = 'active'
ORDER BY wish.id DESC
LIMIT 0,50
Which, would probably be equivalent to this if you don't need the DISTINCT:
SELECT i.*
FROM i
INNER JOIN w
ON w.status = 'active'
AND i.id = wish.id
INNER JOIN r
ON i.id = r.id
AND r.member_id = 1 && r.status = 'active'
ORDER BY i.id DESC
LIMIT 0,50
Please post your schema.
If you are using wish as an existence flag, try:
SELECT i.*, CASE WHEN w.id IS NOT NULL THEN 1 ELSE 0 END AS wish
FROM i
INNER JOIN r
ON i.id = r.id
AND r.member_id = 1 && r.status = 'active'
LEFT JOIN w
ON w.status = 'active'
AND i.id = w.id
ORDER BY wish DESC
LIMIT 0,50
You can use the same technique with a LEFT JOIN to a SELECT DISTINCT subquery. I assume you aren't specifying the w.member_id because you want to know if any members have this? In this case, definitely use the SELECT DISTINCT. You should have an index with id as the first column on w as well in order for that to perform:
SELECT i.*, CASE WHEN w.id IS NOT NULL THEN 1 ELSE 0 END AS wish
FROM i
INNER JOIN r
ON i.id = r.id
AND r.member_id = 1 && r.status = 'active'
LEFT JOIN (
SELECT DISTINCT w.id
FROM w
WHERE w.status = 'active'
) AS w
ON i.id = w.id
ORDER BY wish DESC
LIMIT 0,50
I should have put this in my original question. It's the EXPLAIN as return from SQLYog.
id|select_type|table|type|possible_keys|key|key_len|ref|rows|Extra|
1|PRIMARY|r|ref|member_id,id|member_id|3|const|3120|Using where; Using temporary; Using filesort
1|PRIMARY|i|eq_ref|id|id|8|r.id|1|
2|DEPENDENT SUBQUERY|w|index_subquery|id,status|id|8|func|8|Using where
Please post the EXPLAIN listing. And explain what the tables and columns mean.
wish appears to be a boolean - and you're ORDERing by it?
EDIT: Well, it looks like it's doing what it's being instructed to do. Cade seems to be thinking expansively on what this all could possibly mean (he probably deserves a vote just for effort.) But I'd really rather you tell us.
Wild guessing just confuses everyone (including you, I'm sure.)
OK, based on new info, here's my (slightly less wild) guess.
SELECT i.*,
CASE WHEN EXISTS (SELECT 1 FROM w WHERE id = i.id AND w.status = 'active' THEN 1 ELSE 0 END) AS wish
FROM i
INNER JOIN r ON i.id = r.id AND r.status = 'active'
WHERE r.member_id = 1
Do you want a row for each match in w? Or just to know for i.id , whether there is an active w record? I assumed the second answer, so you don't need to ORDER BY - it's for only one ID anyway. And since you're only returning columns from i, if there are multiple rows in r, you'll just get duplicate rows.
How about posting what you expect to get for a proper answer?
...
ORDER BY wish DESC
LIMIT 0,50
This appears to be the big expense. You're sorting by a computed column "wish" which cannot benefit from an index. This forces it to use a filesort (as indicated by the EXPLAIN) output, which means it writes the whole result set to disk and sorts it using disk I/O which is very slow.
When you post questions like this, you should not expect people to guess how you have defined your tables and indexes. It's very simple to get the full definitions:
mysql> SHOW CREATE TABLE w;
mysql> SHOW CREATE TABLE i;
mysql> SHOW CREATE TABLE r;
Then paste the output into your question.
It's not clear what your purpose is for the "wish" column. The "IN" predicate is a boolean expression, so it always results in 0 or 1. But I'm guessing you're trying to use "IN" in hopes of accomplishing a join without doing a join. It would help if you describe what you're trying to accomplish.
Try this:
SELECT i.*
FROM i
INNER JOIN r ON i.id=r.id
LEFT OUTER JOIN w ON i.id=w.id AND w.status='active'
WHERE r.member_id=1 AND r.status='active'
AND w.id IS NULL
LIMIT 0,50;
It uses an additional outer join, but it doesn't incur a filesort according to my test with EXPLAIN.
Have you tried this?
SELECT i.*, w.id as wish FROM i
LEFT OUTER JOIN w ON i.id = w.id
AND w.status = 'active'
WHERE i.id in (SELECT id FROM r WHERE r.member_id = 1 AND r.status = 'active')
ORDER BY wish DESC
LIMIT 0,50