limiting mysql results by range of a specific key INCLUDING DUPLICATES - mysql

I have a query
SELECT p.*, m.*,
(SELECT COUNT(*) FROM newPhotoonAlert n WHERE n.userIDfor='$id' AND n.threadID=p.threadID and n.seen='0') AS unReadCount
FROM posts p
JOIN myMembers m ON m.id = p.user_id
LEFT JOIN following f
ON (p.user_id = f.user_id AND f.follower_id='$id'
AND f.request='0' AND f.status='1')
JOIN myMembers searcher ON searcher.id = '$id'
WHERE ((f.follower_id = searcher.id) OR m.id='$id')
AND p.flagged <'5'
ORDER BY p.threadID DESC,p.positionID
It brings result as expected but I want to add Another CLAUSE to limit the results.
Say a sample (minimal shown) set of data looks like this with the above query.
threadID postID positionID url
564 1254 2 a.com
564 1245 1 a1.com
541 1215 3 b1.com
541 1212 2 b2.com
541 1210 1 b3.com
523 745 1 c1.com
435 689 2 d2.com
435 688 1 a4.com
256 345 1 s3.com
164 316 1 f1.com
.
.
I want to get ROWS corresponding to 2 DISTINCT threadIDs starting from MAX, but I want to include duplicates as well.
Something like
AND p.threadID IN (Select just Two of all threadIDs currently selected, but include duplicate rows)
So my result should be
threadID postID positionID url
564 1254 2 a.com
564 1245 1 a1.com
541 1215 3 b1.com
541 1212 2 b2.com
541 1210 1 b3.com

You may use something like this:
select threadID, postID, positionID, url
from your_table where
threadID in
(select * from (select distinct threadID from your_table order by threadID desc limit 10) as tab_alias);
Try this syntax to implement it.

Related

SQL Problem regarding to get the sum of duplicate rows

This is my sql query to get the following table below :
select c.name, s.company, p.qty, p.qty * p.price as Total
from client c, purchase p, stock s
where c.clno = p.clno AND s.company = p.company
group by c.name, s.company, p.qty, p.qty * p.price
order by sum(p.qty) desc
The output of the above query looks like this :
Name | Company | Qty | Total
John ABC 12 100
Bob XYZ 10 150
John ABC 5 50
Bob XYZ 20 250
Bob XYZ 2 20
Nav QRS 10 150
John ABC 10 150
I want to have the query to get the output as the following :
Name | Company | Qty | Total
John ABC 27 300
Bob XYZ 32 420
Nav QRS 10 150
As of now your query uses GROUP BY but does not actually aggregates data. You want to GROUP BY name and company, and SUM the quantities and amounts, like :
select c.name, s.company, SUM(p.qty), SUM(p.qty * p.price) as Total
from client c
inner join purchase p on c.clno = p.clno
inner join stock s on s.company = p.company
group by c.name, s.company
order by Total desc
Other remarks regarding your query :
always use explicit joins instead of implicit ones
you can use column aliases in the ORDER BY clause (here, Total ; this can make the query easier to read

Join two tables using mysql

table:tab1
id date_time zoneid accountid slotid trequest bidder width height
_50832 2017-09-04 15:41:06 153 1654 153x468x60 10 aaa 468 60
_50832 2017-09-04 15:41:06 152 1654 152x468x60 10 bbb 468 60
table:tab2
id date_time zoneid accountid slotid bidder count
_50832 2017-09-04 15:41:06 152 1654 152x468x60 bbb 6
_50832 2017-09-04 15:41:06 152 1654 152x468x60 bbb 4
_50832 2017-09-04 15:41:06 153 1654 153x468x60 aaa 9
_50832 2017-09-04 15:41:06 153 1654 153x468x60 aaa 1
below is my query:
SELECT SUM(req.trequest) as REQ, SUM(win.count) as IMP
FROM tab1 as req
JOIN tab2 as win ON (req.id=win.id AND req.zoneid=win.zoneid)
GROUP BY req.zoneid
I get below result,
REQ IMP
20 10
20 10
IMP count is correct but I get wrong REQ count. My expected result is
REQ IMP
10 10
10 10
How to get my expected result?
Lets find the sum of trequest and count separately based on zoneid and id.Then use these two results ( t1 and t2 ) in the inner join.
Count mismatch problem shown in the question occur due to multiple rows satisfying the joining conditions.
In this solution we will only have one entry for each zoneid in both the results ( t1 and t2 ). So the problem is avoided.
Note: You can remove the id column from the GROUP BY clause if it doesn't make any difference.
SELECT t1.id, t1.zoneid, t1.REQ, t2.IMP FROM
(SELECT id,zoneid,SUM(trequest) as REQ
FROM tab1 GROUP BY zoneid,id ) t1
INNER JOIN
(SELECT id,zoneid SUM(win.count) as IMP
FROM tab2 GROUP BY zoneid,id ) t2
ON t1.id = t2.id
AND t1.zoneid = t2.zoneid
Let's try first sumwin.count and group records in sub-query, after it join tables. Try in following:
SELECT SUM(req.trequest) as REQ, SUM(win.count) as IMP
FROM tab1 as req
JOIN (
SELECT SUM(win.count) as IMP, win.zoneid, win.id
FROM tab2 as win
GROUP BY win.zoneid, win.id) AS win ON req.id=win.id AND req.zoneid=win.zoneid
GROUP BY req.zoneid
Instead of req.zoneid. You should try win.zoneid. What seems is that the rows in table 1 are counted multiple times as zoneid in table 2 comes twice. So win.zoneid would group it and avoid the repetition.
Updated: The solution posted by #mayur panchal is the correct one as you don't need to SUM the rows in first table as they belong to different zoneid. If you SUM them you will obviously get the 20 repeated twice.

SQL Count rows in another table and join

I have 2 table in my database. The first is comments and the other is comments_votes.
I want to select all comments, and for each comment, select all it's votes from comments_votes, add them up together and join it with the first query as totalVote.
My comments table look like:
id comment video_id date_sent
----------------------------------------
5 "...." 99 "2017-05-23"
18 "...." 99 "2017-05-23"
comments_votes table look like:
id user_id comment_id vote
----------------------------------------
45 86 5 1
45 23 5 1
78 12 18 -1
And the final wished result would look like:
id comment video_id votes_total
----------------------------------------
5 " ... " 99 2
18 "... " 99 -1
I can manage simple SQL operations but this is beyond me. Is something like this even possible? If yes, how?
select C.id, C.Comment, C.Video_ID, SUM(V.Votes) AS Vote_total
from comments C
left outer join comments_votes V
on C.id=V.comment_id
group by C.id, C.Comment, C.Video_ID
SELECT c.id,comment,c.video_id,SUM(v.vote) AS Vote_total
FROM comments c, comments_votes v
WHERE c.id = v.comment_id
GROUP BY C.id, C.Comment, C.Video_ID;

how to group_concat using distinct correctly - MYSQL

I have 3 table relation using MYSQL;
Example first as riders table:
bib | series_id | point
202 3 200
219 3 140
202 2 200
219 2 110
10 1 90
Example second as series table:
series_id | series_no | season_id
1 1 2
2 2 1
3 1 1
Example third as seasons table:
season_id | year
1 2015
2 2016
How to GROUP_CONCAT point correctly? I'm trying like this
SELECT riders.bib, seasons.year, GROUP_CONCAT(DISTINCT riders.point ORDER BY series.series_no DESC) AS seriPoint
FROM series, riders, seasons
GROUP BY riders.bib
I'm getting output seriPoint for bib: 202 is 200 and bib: 219 is 140,110 when I'm using DISTINCT output like that. But when I'm not using DISTINCT getting output seriPoint for bib: 202 is 200,200,200,200 and bib: 219 is 140,110,140,110. What I want is output seriPoint for bib: 202 is 200,200 and bib: 219 is 140,110.
ADD: please help to add filter too, for season_id when different season_id its to be different row.
yes you are getting correct output since you have used DISTINCT. BTW, you should change your query to use proper JOINS
SELECT riders.bib,
seasons.year,
GROUP_CONCAT(DISTINCT riders.point ORDER BY series.series_no DESC) AS seriPoint
FROM riders
JOIN series ON series.series_id = riders.series_id
JOIN seasons ON series.season_id = seasons.season_id
GROUP BY riders.bib;
(OR) you can get the grouping first and then perform join like
select seasons.year, xx.bib, xx.seriPoint
FROM series
JOIN (
select series_id, bib
group_concat(point) as seriPoint
from riders
group by bib ) xx ON series.series_id = xx.series_id
JOIN seasons ON series.season_id = seasons.season_id
order by xx.seriPoint;

Get total count and total absents in one query

I have 2 tables student and attendance. I need to join both tables and get the result like number of total absents and number of student in particular class and classname. I need a single query to get the below results
tbl_student:
admission_no(PK) student_name student_class
345 John X A
352 Sachin X A
322 Steve IX A
123 Pinky X A
343 Rose IX A
tbl_admission:
admission_no(FK) date_absent
354 2015-03-30
123 2015-03-30
322 2015-03-30
Result should be like this:
Date_absent total_absent total_students student_class
2015-03-30 2 3 X A
2015-03-30 1 2 IX A
you can do something like this to manage it in single query:
SELECT date_absent, total_absent, total_students, a1.student_class
FROM (
SELECT date_absent, count( tbl_admission.admission_no ) AS total_absent, tbl_student.student_class
FROM `tbl_admission`
JOIN `tbl_student` ON tbl_student.admission_no = tbl_admission.admission_no
GROUP BY tbl_student.student_class
)a1
JOIN (
SELECT count( tbl_student.admission_no ) AS total_students, tbl_student.student_class
FROM tbl_student
GROUP BY tbl_student.student_class
)a2
ON a1.student_class = a2.student_class
ORDER BY `a1`.`student_class` DESC