Limit query for each group - mysql

I have this query:
select pl.photo_id, pl.user_id, pl.liker_id, p1.filename user_filename, p2.filename liker_filename
FROM photo_likes pl
left join photos p1 on (pl.photo_id = p1.photo_id)
left join photos p2 on (pl.liker_id = p2.user_id and p2.avatar = 1)
where pl.user_id = $id order by pl.liker_id, pl.date_liked desc
It gets the correct data, but I would like to modify it to limit the data. So, in a nut shell, this query will get all the likes from all the people that liked a photo of theirs, it works great, this can grab lots of photos for each person. But I want to limit it to get only 5 from each person:
So, say user A likes 10 of my photos, user B likes 8 of my photos, and user C likes 2 of my photos, I only want the last 5 from user A, the last 5 from user B and the last 2 from user C. If that makes sense, how can this be done?

The query you have is good, but I'm wrapping that and using MySQL variables to check each return variable and increase the sequence per each "liker". When the liker changes, set the sequence back to 1.... Then, apply HAVING < 6 for the sequence. You can't do it in the WHERE clause because you want EVERY record to be QUALIFIED which keeps updating the #likeSeq and #lastLiker. Only AFTER that is done, the HAVING says... AFTER that, if the seq is greater than you 5 cap, it throws it out.
per alternate rows being included per your print-screens...
select
AllRanks.*
from
( select
PreQualified.*,
#likeSeq := if( PreQualified.Liker_ID = #lastLiker, #likeSeq +1, 1 ) as Seq,
#lastLiker := PreQualified.Liker_ID
from
( select
pl.photo_id,
pl.user_id,
pl.liker_id,
p1.filename user_filename,
p2.filename liker_filename
FROM
photo_likes pl
left join photos p1
on pl.photo_id = p1.photo_id
left join photos p2
on pl.liker_id = p2.user_id
and p2.avatar = 1
where
pl.user_id = $id
order by
pl.liker_id,
pl.date_liked desc ) PreQualified,
( select #lastLiker := 0,
#likeSeq := 0 ) sqlvars
) AllRanks
where
AllRanks.Seq < 6

Could you wrap your joined tables in a sub query?
select ...
from photo_likes
left join photos p1 ...
left join (select p2.filename liker_filename from photos where p1.liker_id = p2.user_id and avatar = 1 LIMIT 5) p2
where ...

Related

Disacrding multiple duplicate records from a left join

I have two tables:-
gallery
gallery_favorite
The user_id in gallery table means the user who posted the item. The user_id in gallery_favorite means the user who added the item in his favorite list. If favorite = 0, then it means the user had initially added the item in favorite list but later removed it.
Now, I want to fetch all the gallery items along with its favorite status. Here is my query:-
Select distinct `gallery`.`id`, `gallery`.`caption`, `gallery`.`type`,
`gallery`.`video`, `gallery`.`image`, `gallery`.`type`,
`gallery`.`created_date`, `gallery`.`modified_date`,
`gallery_favorite`.`favorite`, `gallery`.`user_id`
from `gallery`
left join `gallery_favorite` on `gallery_favorite`.`gallery_id` = `gallery`.`id`
where
(`gallery`.`type` = 'i'
and `gallery`.`status` = 1
and `gallery`.`deleted` = 0)
and
((`gallery`.`user_id` != 11 and `gallery`.`private` = 0)
or `gallery`.`user_id` = 11)
limit 20 offset 0
But a syou can see, I am getting duplicate records depending upon the number of rows wrt to a gallery item in the gallery favorite table. How can I modify the query to get only one record (along with my own favorite status)?
I guess you are getting duplicate records because you have not joined both the table on user_id -
Try below query -
Select distinct `gallery`.`id`, `gallery`.`caption`, `gallery`.`type`,
`gallery`.`video`, `gallery`.`image`, `gallery`.`type`,
`gallery`.`created_date`, `gallery`.`modified_date`,
`gallery_favorite`.`favorite`, `gallery`.`user_id`
from `gallery`
left join `gallery_favorite` on `gallery_favorite`.`gallery_id` = `gallery`.`id`
and `gallery_favorite`.`user_id` = `gallery`.`user_id`
where
(`gallery`.`type` = 'i'
and `gallery`.`status` = 1
and `gallery`.`deleted` = 0)
and
((`gallery`.`user_id` != 11 and `gallery`.`private` = 0)
or `gallery`.`user_id` = 11)
limit 20 offset 0
Assuming the gallery as many favorites from different users. It makes no sense to display exclusively a hit favorite to a user and gallery alone. Counting them makes sense.
Select `gallery`.`id`, `gallery`.`caption`, `gallery`.`type`,
`gallery`.`video`, `gallery`.`image`, `gallery`.`type`,
`gallery`.`created_date`, `gallery`.`modified_date`,
sum(gallery_favorite`.`favorite`) as total_favorites -- count them group function aggregate
from `gallery`
left join `gallery_favorite` on `gallery_favorite`.`gallery_id` = `gallery`.`id`
where
(`gallery`.`type` = 'i'
and `gallery`.`status` = 1
and `gallery`.`deleted` = 0)
and
((`gallery`.`user_id` != 11 and `gallery`.`private` = 0)
or `gallery`.`user_id` = 11)
and gallery_favorite`.`favorite` = 1 -- count only the favorites
GROUP BY `gallery`.`id` -- GROUP CLAUSE
This is how a join works. You get all rows matching the condition. You can either group the result by the fields in the left table (thus eliminating duplicates in the output) or join with a table that has one entry per gallery item -- this requires joining with a (SELECT ... FROM gallery_favorite GROUP BY gallery_id)

Mysql SUM based on condition

I need help to formulate a query to calculate points based on user action.
There is a user table which stores user info and each user belongs to a specific category (User category). Each category has different levels (say level1, level2 etc..) and each level has a set of tasks(Task1,Task2...) to be completed. Each task can be performed multiple times. But points is calculated based on a maximum limit field.
Task 1 - 10 points ( Max:2 - Means user can perform this task N times but for point calculation it will be only counted twice )
Task 2 - 5 points ( Max:1 )
For example lets say User1 performs Task1 5 times, but for point calculation Task1 will only be counted 2 times , so total points is 20. This is similar for all tasks.
On completing each task the user gains N points and get upgraded to next level.
Please find below the table structure and query:
users
uid , uname , uc_id ( References User Category ) , ul_id (References user level and indicates current level of user )
user_category
uc_id ....
user_level
ul_id, uc_id (References User Category)
...
level_tasks
lt_id , ul_id (References User level) , lt_point, lt_max
user_tasks
ut_id, lt_id (References level task), uid (References user)
The below is the query i have tried so far :
SELECT uid, SUM(LT.lt_point/LT.lt_max) as TotalLevelPoints
FROM users AS U
INNER JOIN user_tasks AS UT ON UT.ut_user_id = U.id
INNER JOIN level_tasks AS LT ON LT.lt_id = UT.lt_id
INNER JOIN user_level AS UL ON UL.ul_id = LT.ul_id
INNER JOIN user_category AS UC ON UC.uc_id = UL.uc_id
WHERE UT.ut_status = 1 AND U.uid = 1 AND UL.ul_id = 1
GROUP BY U.uid
This gives all the sum without considering the max limit, how should i write so that sum is calculated on that column also.
Hope my question is clear enough.. Any help would be really appreciated.
I believe you will need to do this in 2 steps. First sum the points and compare to the maximums allowed for each task type. Once that is determined, sum those results for each user. e.g.
SELECT
u.uid
, SUM(user_task_points) AS user_task_points
FROM users AS u
INNER JOIN (
SELECT
ut.UT_user_id
, lt_id
, lt.LT_point
, lt.LT_max_times
, CASE WHEN SUM(lt.LT_point) > (lt.LT_point * lt.LT_max_times) THEN (lt.LT_point * lt.LT_max_times)
ELSE SUM(lt.LT_point)
END AS user_task_points
FROM user_tasks AS ut
INNER JOIN level_tasks AS lt ON ut.UT_rule_id = lt.LT_id
INNER JOIN user_level AS UL ON UL.ul_id = LT.ul_id
WHERE ut.UT_status = 1
AND ut.UT_user_id = 1
AND ul.ul_id = 1
GROUP BY
ut.UT_user_id
, lt_id
, lt.LT_point
, lt.LT_max_times
) AS p ON u.id = p.UT_user_id
GROUP BY
u.uid
;
There is some discrepancy between column names in schema and query you have provided. I have gone with schema.
Query is not tested. But this approach should work.
SELECT uid, SUM(LevelPoints) TotalLevelPoints
FROM ( SELECT U.uid, LT.lt_id, SUM((CASE WHEN COUNT(*) > lt_max THEN lt_max ELSE COUNT(*) END)*lt_point) LevelPoints
FROM users U
INNER JOIN user_tasks UT
ON U.uid = UT.uid
INNER JOIN level_tasks LT
ON UT.lt_id = LT.lt_id
GROUP BY U.uid, LT.lt_id
)
GROUP BY uid

SQL Query with drop or null on statement

On a Userpage we show Awardpictures recived from 2 sql tables.
In zg15_auszeichnungen_vergeben which contains all user awards in form from award type id`s, award dates ... we read over the users pilotID the award datas.
Over a LEFT JOIN from zg15_auszeichnungen_main we get the Awardpicture and the Award Name which shows on the page.
SELECT v.pilotID, v.auszBereichID, v.auszTypID, v.gameID, v.auszDatum, m.mainGrafik, m.mainName
FROM zg15_auszeichnungen_vergeben
LEFT JOIN zg15_auszeichnungen_main m ON v.auszMainID = m.mainID
WHERE v.pilotID = '$pilotID'
AND v.auszBereichID = 6
AND v.auszTypID = 100
AND v.gameID = 1
ORDER BY v.auszDatum;
Because there are to many year awards(1 year,2years,3years...12 years) together wit some other awards, i want just show the oldest award of the years (just 12 years instead of all). The year awards have the value 11 from m.mainKatID and the oldest has the highest value from m.mainPunkte.
Other awards like (most Postpoints) have other values (10) in mainKatID.
I'm not familiar with mysql and don't get the result i want.
Change your ORDER BY to ORDER BY m.mainKatID DESC LIMIT 3 and set limit to return 3 records.
SELECT
v.pilotID, v.auszBereichID, v.auszTypID, v.gameID, v.auszDatum,
m.mainGrafik, m.mainName FROM zg15_auszeichnungen_vergeben v
LEFT JOIN zg15_auszeichnungen_main m ON v.auszMainID = m.mainID
WHERE v.pilotID = '$pilotID'
AND v.auszBereichID = 6
AND v.auszTypID = 100
AND v.gameID = 1
ORDER BY m.mainKatID DESC LIMIT 3
Use a subquery that gets the oldest year award for each mainID. Combine that with the non-year awards using UNION.
SELECT v.pilotID, v.auszBereichID, v.auszTypID, v.gameID, v.auszDatum, m.mainGrafik, m.mainName
FROM zg15_auszeichnungen_vergeben AS v
LEFT JOIN (
SELECT * FROM zg15_auszeichnungen_main
WHERE mainKatID != 11
UNION
SELECT m1.*
FROM zg15_auszeichnungen_main AS m1
JOIN (SELECT mainID, MAX(mainPunkte) AS maxPunkte
FROM zg15_auszeichnungen_main
WHERE mainKatID = 11
GROUP BY mainID) AS m2
ON m1.mainID = m2.mainID AND m1.mainPunkte = m2.maxPunkte
WHERE m1.mainKatID = 11) AS m
ON v.auszMainID = m.mainID
WHERE v.pilotID = '$pilotID'
AND v.auszBereichID = 6
AND v.auszTypID = 100
AND v.gameID = 1
ORDER BY v.auszDatum;

combine tables with 1 to N relationship into 1 line of record with the last value of the N record

I need a modification of my previous post regarding
how to combine tables with 1 to many relationship into 1 line of record
how to combine tables with 1 to many relationship into 1 line of record
now my problem is my record has now 1 to many relationship. What I need to show is the last record only and combine it in a single line
tables tbl_equipment and tbl_warranty
and here is the desired output
here is the code I'm trying to implement
SELECT
a.equipmentid,
a.codename,
a.name,
a.labelid,
a.ACQUISITIONDATE,
a.description,
a.partofid,
w1.warrantyid as serviceidwarranty,
w1.startdate,
w1.enddate,
w2.warrantyid as productidwarranty,
w2.startdate,
w2.enddate,
s.equipstatusid,
l.equiplocationid FROM TBL_EQUIPMENTMST a
left JOIN tbl_equipwarranty w1
ON w1.equipmentid=a.equipmentid and w1.serviceproduct = 'service'
left JOIN tbl_equipwarranty w2
ON w2.equipmentid=a.equipmentid and w2.serviceproduct = 'product'
left join tbl_equipstatus s
on a.equipmentid = s.equipmentid
left join tbl_equiplocation l
on a.equipmentid = l.equipmentid WHERE a.equipmentid = '112'
I only want to show 1 record with the last value of warranty product and warranty service in the output. Can anyone guide me how to modify my code so that when I try join all the tables listed above can produce 1 record only with the last record of warranty as an output.
I am using firebird as a database. If you have a solution in mysql kindly tell me and ill try to find the counterpart in firebird.
with summary as(
select e.equipmentid ,e.Codename,e.Name,w.warrantyid ,w.Satartdate ,w.Enddate,w.warrantytype
from Eqp e
join Warranty w
on(w.equipmentid =e.equipmentid )
where w.warrantyid =3)
select *,w.warrantyid,w.Satartdate ,w.Enddate,w.warrantytype
from summary s
join Warranty w
on s.Satartdate =w.Satartdate and s.Enddate =w.Enddate
where w.warrantyid =4
after reading the comment of Barmar at the question for solution. I Figured out subquery can solve my problem. Subquery is a new word for me. I research on how to use subquery and came out with a solution below. you can correct me if my code is wrong or how to improve the performance of the query
SELECT
a.equipmentid,a.codename,a.name,a.labelid,a.ACQUISITIONDATE,a.description,a.partofid,
w1.warrantyid as serviceidwarranty,w1.startdate,w1.enddate,
w2.warrantyid as productidwarranty,w2.startdate,w2.enddate,
s.equipstatusid,
l.equiplocationid
FROM
TBL_EQUIPMENTMST a
left JOIN
(select first 1 *
from tbl_equipwarranty
where equipmentid='112' and serviceproduct = 'service'
order by warrantyid desc) w1 ON w1.equipmentid = a.equipmentid
and w1.serviceproduct = 'service'
left JOIN
(select first 1 *
from tbl_equipwarranty
where equipmentid = '112' and serviceproduct = 'product'
order by warrantyid desc) w2 ON w2.equipmentid = a.equipmentid
and w2.serviceproduct = 'product'
left join
(select first 1 *
from tbl_equipstatus
where equipmentid = '112'
order by equipstatusid desc) s on a.equipmentid = s.equipmentid
left join
(select first 1 *
from tbl_equiplocation
where equipmentid = '112'
order by equiplocationid desc) l on a.equipmentid = l.equipmentid
WHERE
a.equipmentid = '112'

MySQL SELECT DISTINCT ORDER BY problem

To begin with I have 4 tables I am dealing with.
I have a classes table that is a 1->N relationship with a sections table which also has a 1->N relationship with a lessons table.
So to put it in perpective:
Classes
Sections
Lessons
The last table is an activityLog, when the student accesses a lesson this is recorded using the following:
ActivityLog Row -> actorID (user ID), classID, sectionID, lessonID
I want to pull out the last 5 unique lessons the student has visited. I tried using both DISTINCT and GROUP BY without success.
The same records are being returned each time, not the latest classes that they have visited.
Using GROUP BY
SELECT activityLog.actorID, activityLog.activityDate,
strClasses.classID, strClasses.className,
strSections.sectionID, strSections.sectionName,
strLessons.lessonID, strLessons.lessonName
FROM activityLog
LEFT JOIN strClasses ON strClasses.classID = activityLog.classID
LEFT JOIN strSections ON strSections.sectionID = activityLog.sectionID
LEFT JOIN strLessons ON strLessons.lessonID = activityLog.lessonID
WHERE activityLog.activityTypeID = 6 AND activityLog.actorID = 3
GROUP BY activityLog.lessonID
ORDER BY activityLog.activityDate DESC
LIMIT 5
Using DISTINCT
SELECT DISTINCT activityLog.actorID,
strClasses.classID, strClasses.className,
strSections.sectionID, strSections.sectionName,
strLessons.lessonID, strLessons.lessonName
FROM activityLog
LEFT JOIN strClasses ON strClasses.classID = activityLog.classID
LEFT JOIN strSections ON strSections.sectionID = activityLog.sectionID
LEFT JOIN strLessons ON strLessons.lessonID = activityLog.lessonID
WHERE activityLog.activityTypeID = 6 AND activityLog.actorID = 3
ORDER BY activityLog.activityDate DESC
LIMIT 5
I cannot figure out why the latest records are not being displayed.
Based on your change, how does this suit you?
SELECT activityLog.actorID, activityLog.activityDate,
strClasses.classID, strClasses.className,
strSections.sectionID, strSections.sectionName,
strLessons.lessonID, strLessons.lessonName
FROM activityLog
LEFT JOIN strClasses ON strClasses.classID = activityLog.classID
LEFT JOIN strSections ON strSections.sectionID = activityLog.sectionID
LEFT JOIN strLessons ON strLessons.lessonID = activityLog.lessonID
WHERE activityLog.activityTypeID = 6 AND activityLog.actorID = 3
AND activityLog.activityDate = (SELECT MAX(activityDate) FROM activityLog AS lookup WHERE lessonID = activityLog.lessonID)
ORDER BY activityLog.activityDate DESC
LIMIT 5
Based on your description, I'm not sure why you're using LEFT JOIN, but I've left it in just in case.
Try group by like below
GROUP BY activityLog.classID,activityLog.sectionID,activityLog.lessonID
I think it will work, or just sent me create scripts for these I will create that query
Well, there's got to be a datetime in the ActivityLog I hope... so Try this:
Select s.Name, c.ClassName
From Students s
left Join On Classes c
On c.ClassId In
(Select Distinct ClassId From Classes
Where (Select Count(Distinct ClassId) From Classes ic
Join ActivityLog l On l.UserId = s.UserId
And l.ClassId = c.ClassId
Where classId = c.ClassId
And activityDateTime > l.activityDateTime)
< 5)