Getting the last 5 records of a joined query - mysql

I have a query that does what i want joining table but i need it to change sligtly so i can use it for something else.
I need to get the last 5 records so i should be using the max function and limit it to 5 but it's not working properly
This is my current query, just need to get the last 5 records (probably by the festivalid)
SELECT f.*,
v.total,
v.votes,
v.festivalid,
ifnull(r.reviewcount,0) as count
FROM festivals f
INNER
JOIN vote v
ON f.festivalid = v.festivalid
LEFT OUTER
JOIN (SELECT festivalid,
count(*) as reviewcount
FROM reviews
GROUP BY festivalid) as r
ON r.festivalid = v.festivalid
WHERE f.datefrom > CURRENT_TIMESTAMP
ORDER BY f.datefrom, f.eventname

ORDER BY f.datefrom DESC, f.eventname DESC
Limit 5

Related

sql query group by not working in subquery

I have a sql query based on 2 IDs in the same table, the results of the sum have come out correctly, but the problem is group by cannot handle data that appears 2 times
SELECT coa_a.debet_april,coa_a.namacoaapril, coa_b.kredit_april ,coa_b.namacoaapril
FROM `t_jurnalumum` join coa on coa.m_coa_4_id=t_jurnalumum.IdDebet
join (select DISTINCT m_coa_4_id, sum(a.Nilai) as debet_april, coa.namacoa as namacoaapril
from t_jurnalumum a j
join coa on a.IdDebet=coa.m_coa_4_id
where year (a.Tanggal)=2021 and month (a.Tanggal)=4
GROUP by a.IdDebet ) as coa_a on coa_a.m_coa_4_id=t_jurnalumum.IdDebet
join (select DISTINCT m_coa_4_id, sum(b.Nilai) as kredit_april, coa.namacoa as namacoaapril
from t_jurnalumum b
join coa on b.IdKredit=coa.m_coa_4_id
where year (b.Tanggal)=2021 and month (b.Tanggal)=4
GROUP by b.IdKredit ) as coa_b on coa_b.m_coa_4_id=t_jurnalumum.IdKredit
GROUP by coa_b.namacoaapril, coa_a.namacoaapril
this the result
and this is the main table
It is because you are using the same table. The first sub query of join will give you the distinct value from first group by and second group by gives distinct value. And at the end you also have grouped the whole result. The first one gives two distinct value and second one gives two distinct value which eventually gives all value.

MySQL query that limits first join to specific number of rows

I'm trying to run a query that joins 3 tables and I want to limit the first join to just 5 rows. The end result can return any number of rows so I don't want to add LIMIT to the end of the query.
Here is the query I have, which works but obviously does not limit the first join to 5 rows. I've attempted a subquery, which I believe is the only way to accomplish this, and everything I try gives an error. I can't seem to apply examples I have seen, to my situation.
SELECT mw_customer.customer_id, mw_customer.customer_uid, mw_campaign.customer_id, mw_campaign.campaign_id, mw_campaign.type, mw_campaign.status, mw_campaign_delivery_log.campaign_id, mw_campaign_delivery_log.subscriber_id
FROM mw_customer
JOIN mw_campaign
ON mw_customer.customer_id = mw_campaign.customer_id
AND mw_customer.customer_uid = 'XYZ'
AND mw_campaign.type = 'regular'
AND mw_campaign.status = 'sent'
JOIN mw_campaign_delivery_log
ON mw_campaign.campaign_id = mw_campaign_delivery_log.campaign_id
So what I want to do is limit the "JOIN mw_customer" to a maximum of 5 rows and then after the JOIN mw_campaign_delivery_log, there can be any number of rows.
Thanks
Wrap the first join in a subquery with LIMIT 5.
SELECT t.customer_id, t.customer_uid, t.campaign_id, t.type, t.status, l.subscriber_id
FROM (SELECT cus.customer_id, cus.customer_uid, cam.campaign_id, cam.type, cam.status
FROM mw_customer AS cus
JOIN mw_campaign AS cam
ON cus.customer_id = cam.customer_id
WHERE cus.customer_uid = 'XYZ'
AND cam.type = 'regular'
AND cam.status = 'sent'
LIMIT 5) AS t
JOIN mw_campaign_delivery_log AS l
ON t.campaign_id = l.campaign_id
Note that LIMIT without ORDER BY means that the 5 rows selected will be unpredictable.

Query optimisation for order BY DESC

Total number of records in table "taking" = 12 000.
I have below query in which only need 10 records which is recently or last added records. So I apply OrderBy desc but when I apply DESC using ORDER BY on bid then it will hang and query will return output in 15 mins for 10 records.
SELECT taking.BId ,taking.calledDate Called,taking.calleduser Caller,taking.callerNotes,taking.Title,
taking.FName,taking.ScName,taking.takingPhase, Training.TName,
taking.TrDate,taking.SubmitDateTime, TrLocation.TrLocation trTrl,
taking.STATUS,taking.DelegNum, taking.Tel, taking.Email,taking.Notes
FROM Taking taking
LEFT JOIN training Training ON taking.Training = Training.TId
LEFT JOIN confirmation ON taking.Bid = confirmation.Bid
LEFT JOIN TrLocation ON taking.TrLocation = TrLocation.TrLId
LEFT JOIN Invoice ON taking.BId =Invoice.BId
LEFT JOIN couriertracking ON taking.BId = couriertracking.bId
LEFT JOIN SalesPerson ON taking.SId = SalesPerson.SId WHERE
taking.SubmitDateTime > DATE_SUB(NOW(), INTERVAL 2 MONTH) AND IFNULL(taking.SId,0) > 0
ORDER BY taking.BId DESC LIMIT 10
I tried Indexing at bid column but still query time is same. How can i optimise the query?
Since you only have LEFT JOINs, and the LEFT JOIN can only add more rows, you can limit the number of rows returned from the Taking table with a subquery. Then only 10 rows need to be joined with the other tables. If any JOIN will add more rows, you can still use another LIMIT in the outer query.
SELECT
taking.BId,taking.calledDate Called, taking.calleduser Caller,
taking.callerNotes, taking.Title, taking.FName, taking.ScName,
taking.takingPhase, Training.TName, taking.TrDate,
taking.SubmitDateTime, TrLocation.TrLocation trTrl, taking.STATUS,
taking.DelegNum, taking.Tel, taking.Email,taking.Notes
FROM (
SELECT taking.*
FROM Taking taking
WHERE taking.SubmitDateTime > DATE_SUB(NOW(), INTERVAL 2 MONTH)
AND taking.SId > 0
ORDER BY taking.BId DESC
LIMIT 10
) taking
LEFT JOIN training Training ON taking.Training = Training.TId
LEFT JOIN confirmation ON taking.Bid = confirmation.Bid
LEFT JOIN TrLocation ON taking.TrLocation = TrLocation.TrLId
LEFT JOIN Invoice ON taking.BId =Invoice.BId
LEFT JOIN couriertracking ON taking.BId = couriertracking.bId
LEFT JOIN SalesPerson ON taking.SId = SalesPerson.SId
ORDER BY taking.BId DESC
LIMIT 10
Note that taking.SId > 0 has the same effect as IFNULL(taking.SId,0) > 0 in the WHERE clause. Rows with NULL in SId will not match that condition.
However - You should still verify that you have indexes on every column which are used for the joins in every table. For this query you will need indexes on
Training.TId
confirmation.Bid
TrLocation.TrLId
Invoice.BId
couriertracking.bId
SalesPerson.SId
Some of them look like primary keys. If that's the case then you don't need to index them.
To find the best index for the Taking table is not that simple. But 12K rows is not that much. You should be fine just with an index on SubmitDateTime.

Multi Join + Groub By, get 1 row per album with max rating

im trying to make a chart list and i use the following query
SELECT
vmm_user.username,
vmm_songs.*,
vmm_albums.desc,
vmm_albums.release,
vmm_albums.name,
AVG(vmm_songrating.rating) AS ratingavg,
COUNT(vmm_songrating.id) AS ratingcount
FROM
vmm_songs
LEFT JOIN
vmm_user
ON
vmm_songs.userid=vmm_user.id
LEFT JOIN
vmm_albums
ON
vmm_songs.albumid=vmm_albums.id
LEFT JOIN
vmm_songrating
ON
vmm_songs.id=vmm_songrating.songid
GROUP BY
vmm_songs.id
HAVING
COUNT(vmm_songrating.id) >= 2
ORDER BY
AVG(vmm_songrating.rating) DESC
LIMIT
10
As you can see i get every entry with at least 2 ratings but i only want to show the songs with the max average rating and minimum 2 ratings per album
i tried to add something like
MAX(AVG(vmm_songrating.rating))
in the having clause but it didnt work
i use php and mysql
Thanks for helping
EDIT 3:
mysql fiddle
http://sqlfiddle.com/#!2/677cf/1/0
just want the first 3 rows (song with highest rating from the album)
Try something like below..
SELECT MAX(songs.ratingavg), [Other Columns]
FROM (
SELECT
vmm_user.username,
vmm_songs.*,
vmm_albums.desc,
vmm_albums.release,
vmm_albums.name,
AVG(vmm_songrating.rating) AS ratingavg,
COUNT(vmm_songrating.id) AS ratingcount
FROM
vmm_songs
LEFT JOIN
vmm_user
ON
vmm_songs.userid=vmm_user.id
LEFT JOIN
vmm_albums
ON
vmm_songs.albumid=vmm_albums.id
LEFT JOIN
vmm_songrating
ON
vmm_songs.id=vmm_songrating.songid
GROUP BY
vmm_songs.id
HAVING
COUNT(vmm_songrating.id) >= 2
ORDER BY
AVG(vmm_songrating.rating) DESC
LIMIT
10 ) AS songs

Query output differs from the expected output

Below query is doing what I need:
SELECT assign.from_uid, assign.aid, assign.message, curriculum.asset,
curriculum.title, curriculum.description
FROM assignment assign
INNER JOIN curriculum_topics_assets curriculum
ON assign.nid = curriculum.asset
WHERE assign.to_uid = 13 AND assign.status = 1
GROUP BY assign.from_uid, assign.to_uid, assign.nid
ORDER BY assign.created DESC
Now I need to get the total count of rows of the result. For example if it is displaying 5 rows the o/p should be like My expected o/p. The query I tried is given below.
SELECT count(description) FROM assignment assign
INNER JOIN curriculum_topics_assets curriculum ON assign.nid = curriculum.asset
WHERE assign.to_uid = 13 AND assign.status = 1
GROUP BY assign.from_uid, assign.to_uid, assign.nid
ORDER BY assign.created DESC
My expected o/p:
count(*)
---------
5
My current o/p:
count(*)
---------
6
2
5
6
6
The easiest solution would be to
place your initial GROUP BY query in a subselect
select the amount of rows retrieved from this subselect
SQL Statement
SELECT COUNT(*)
FROM (
SELECT assign.from_uid
FROM assignment assign
INNER JOIN curriculum_topics_assets curriculum ON assign.nid = curriculum.asset
WHERE assign.to_uid = 13
AND assign.status = 1
GROUP BY
assign.from_uid
, assign.to_uid
, assign.nid
) q
Edit - why doesn't the original query return the results required
It did already prepared what was needed to get the correct result
Your query without grouping returns a resultset of 25 records (6+2+5+6+6)
From these 25 records, you have 5 unique combinations of from_uid, to_uid, nid
Now you don't want to count how many records each combination has (as you did in your example) but how many unique (distinct anyone?) combinations there are.
One solution to this is the subselect I presented but following equivalent statement using a DISTINCT clause might be more comprehensive.
SELECT COUNT(*)
FROM (
SELECT DISTINCT assign.from_uid
, assign.to_uid
, assign.nid
FROM assignment assign
INNER JOIN curriculum_topics_assets curriculum ON assign.nid = curriculum.asset
WHERE assign.to_uid = 13
AND assign.status = 1
) q
Note that my personal preference goes to the GROUP BY solution.
To get the number of rows for a query do:
SELECT COUNT(*) as RowCount FROM (--insert other query here--) s
In you example:
SELECT COUNT(*) as RowCount FROM (SELECT a.from_uid
FROM assignment a
INNER JOIN curriculum_topics_assets c ON a.nid = c.asset
WHERE a.to_uid = 13
AND a.status = 1
GROUP BY a.from_uid, a.to_uid, a.nid
) s
Note that I the dropped the stuff that has no effect on the number of rows to make the query run slightly faster.
You should use COUNT(*) instead of count(description). Look at: http://www.mysqlperformanceblog.com/2007/04/10/count-vs-countcol/