I have two tables in the mysql database:
trips(id,name,desc,createdat)
trip_ratings(id,ratingvalue,tripid,userid)
I want to get 100 most recent trips sorted by higher average ratings.
I have tried following mysql query:
SELECT AVG(ratingvalue),tripid,tripcreatedat FROM trip_ratings
INNER JOIN trips on trip_ratings.tripid = trips.id
GROUP BY trip_ratings.tripid
ORDER BY AVG(ratingvalue) DESC, tripcreatedat DESC
LIMIT 100
But as it sorts by rating value first I only get trips sorted by higher ratings.
Is it possible within a single query? can anyone hint me what should I do?
EDIT: example:
I have data in trip_ratings table like this:
and from my tried query I can get results like this.
But my problem is: Get 100 most recent trips sorted by higher average ratings.
Instead of downvoting and close votes, can anyone have any solution for that or can anyone give me a hint that is it possible within a single query? thanks.
One option is to use an inline view, to first get the "100 most recent trips".
Then join that to trip_ratings, to calculate the "average rating" and order by that result.
SELECT m.id AS tripid
, AVG(r.ratingvalue) AS average_rating
, m.tripcreatedat
FROM ( SELECT t.id
, t.tripcreatedat
FROM trips t
ORDER BY t.tripcreatedat DESC
LIMIT 100
) m
LEFT
JOIN trip_ratings r
ON r.tripid = m.id
GROUP BY m.id, m.tripcreatedat
ORDER BY AVG(r.ratingvalue) DESC
If there are multiple trips that have the same average rating, it's indeterminiate what order those will be returned in. You can add other expressions to the order by to make it more deterministic.
ORDER BY AVG(r.ratingvalue) DESC, m.tripcreatedat DESC, m.id DESC
This isn't the only way to do it. There are other approaches that will achieve an equivalent result.
Related
I'm trying to figure out how to do this on my own but it looks like a dead end to me.
I am working under wordpress framework and with some custom tables.
The result i am trying to achieve is very simple but the way to get there is just too much for my head right now.
I need to select the top 50 results from tableOne based on the ammount of times that the id from tableOne is mentioned in tableTwo under some simple where conditions.
Using $wpdb class for the latest WordPress build, what can i use to achieve this?
Thanks
This is the Simple tableOne Query to get all posts:
$allPosts = $wpdb->get_results("SELECT * FROM pokeGrid_images WHERE status='0' ORDER BY tempo DESC LIMIT ".$limit." OFFSET ".$offset."");
Now i need the first 50 results from this table, based on the number of times their id is mentioned on the second table with this structure: http://prntscr.com/byz2qw
Edit:
http://prntscr.com/byz79b
Note: Basically this is a forum, table one has the posts, table 2 the upvotes.
The expression must gather the most upvoted posts for the last X days.
If it was 1 post the expression would be Select Count(*) FROM tableTwo Where tempo > then ".$variableWithUnixTimeDIff."
column tempo is a now() timestamp.
Thanks
Note: Basically this is a forum, table one has the posts, table 2 the
upvotes. The expression must gather the most upvoted posts for the
last X days. If it was 1 post the expression would be Select Count(*)
FROM tableTwo Where tempo > then ".$variableWithUnixTimeDIff."
SELECT *, Sum(score) AS totalScore FROM tableTwo INNER JOIN tableOne ON tableTwo.memeID = tableOne.id GROUP BY memeID ORDER BY totalScore DESC;
Try this query.
//Edit - I just created a sample table with my own data. I only created the tableTwo, where this query did work:
SELECT *, Sum(score) AS totalScore FROM tableTwo GROUP BY memeID ORDER BY totalScore DESC
So after that, just inner join data from tableOne and it will work!
Here is my SQL
SELECT DISTINCT(scl_reviews.locid),avg(scl_reviews.rating)
AS average,scl_locations.name,scl_locations.phone,scl_locations.address,
scl_location.city,scl_locations.state,scl_locations.zip
FROM scl_reviews
LEFT JOIN scl_locations ON scl_locations.locid = scl_reviews.locid
GROUP BY scl_reviews.locid ORDER BY average DESC LIMIT 100
What this means is that it will grab locations with the highest rating and sort them by the average.
The problem is that the records with an average of 5 seem to query their positions differently. Sometimes my record with an id of 3115 is position 1 and sometimes its position 3.
Not quite sure whats going on,. I assume it has to do with my query.
ORDER BY average DESC leaves undetermined how the order will be when there are equal values. So just list a unique column in the sort order. You might like the column number short-cut:
ORDER BY 2 DESC, 1 DESC
Add a secondary order. This kicks in when your first order has equal values.
Check the new order by clause:
SELECT DISTINCT(scl_reviews.locid),avg(scl_reviews.rating)
AS average,scl_locations.name,scl_locations.phone,scl_locations.address,
scl_location.city,scl_locations.state,scl_locations.zip
FROM scl_reviews
LEFT JOIN scl_locations ON scl_locations.locid = scl_reviews.locid
GROUP BY scl_reviews.locid
ORDER BY average DESC, scl_reviews.locid DESC LIMIT 100
My search query runs like:
select * from posts p where p.post like '%test%' ORDER BY p.upvotes DESC,
p.unix_timestamp DESC LIMIT 20
If there are more than 20 results for the searched keyword, i find out the minimum timestamp value, store it in a hidden element and run another query to Load More results like:
select * from posts p where p.post like '%test%' and p.unix_timestamp < 1360662045
ORDER BY p.upvotes DESC, p.unix_timestamp DESC LIMIT 20
Whats really happening is that my first query is ignoring (Obviously, my mistake) posts which haven't had any votes(meaning 0 votes) because of my ORDER BY p.upvotes DESC and as a result of this, i noticed that it fetched the first post in the table in the first 20 results, so the minimum timestamp becomes first post's timestamp. Now after this, if i try to fetch the next 20 results which is less than the minimum timestamp, it doesn't give anything.
Right now, i am simply using the upvotes ordering to fetch top records. Should i be using some algorithm like Bayesian Average or some other algorithm?
Please advise how i can improve the queries if i had to stay with current system of ordering or is there any viable and more efficient method i should be using?
P.S. If possible, please refer some resources about the Bayesian Average(it seems to be most used) or some other alternative?
Storing the timestamp when you first do a search and then using that for the next query you could use something like this:-
$sql = "SELECT *
FROM posts p
LEFT OUTER JOIN (SELECT post_id, COUNT(*) FROM post_ratings WHERE timestamp_rated <= $SomeTimeStoredBetweenPages GROUP BY post_id) pr ON p.id = pr.post_id
WHERE p.post like '%test%'
ORDER BY pr.post_ratings DESC, p.unix_timestamp
DESC LIMIT ".(($PageNo - 1) * 20)." 20";
This is very much an example as I have no real idea of you table structures. Also not sure if you just have a row for each up vote, or whether there are down votes to take account of as well.
I have following query and I am trying to display members in the order where members with highest average rating will be displayed first, if more than one members have same average rating then highest number of rating will be considered.
Here,
Member A has been rated by 3 visitors and average rating value is 5 while
Member B has been rated by 2 visitors and average rating value is 5
So according to below query, Member A should display first because he has 5 average rating and rated by 3 persons while Member B should display on second position.
But Member B is displaying first and Member A is displaying second so this is problem. Please let me know what wrong I am doing in query.
SELECT m.*,mc.*
FROM t_member m
LEFT JOIN tr_member_category mc ON m.memberpkid=mc.memberpkid
LEFT JOIN tr_comment c
ON m.memberpkid=c.memberpkid
AND c.approved='YES' AND c.visible='YES'
WHERE m.visible='YES' AND m.approved='YES'
AND m.gender='FEMALE' AND mc.archivecatpkid=1
GROUP BY m.memberpkid
ORDER BY avg(c.ratingvalue) DESC, COUNT(c.ratingvalue) DESC
Thank you very much in advance,
KRA
Move the AVG and COUNT functions out of the ORDER clause, into the SELECT clause. Give them a good name and then order on those.
i.e.
SELECT m.*, mc.*, AVG(c.ratingvalue) AS average_rating, COUNT(c.ratingvalue) AS number_of_ratings
FROM t_member m
LEFT JOIN tr_member_category mc ON m.memberpkid=mc.memberpkid
LEFT JOIN tr_comment c
ON m.memberpkid=c.memberpkid
AND c.approved='YES' AND c.visible='YES'
WHERE m.visible='YES' AND m.approved='YES'
AND m.gender='FEMALE' AND mc.archivecatpkid=1
GROUP BY m.memberpkid
ORDER BY average_rating DESC, number_of_ratings DESC
EDIT: remember that an aggregate function returns a value. If you put them in an ORDER BY clause, it reads "order by this value", as opposed to "order by this column". Keep that in mind whilst writing SQL.
dear php and mysql expertor
i have two table one large for posts artices 200,000records (index colume: sid) , and one small table (index colume topicid ) for topics has 20 record .. have same topicid
curent im using : ( it took round 0.4s)
+do get last 50 record from table:
SELECT sid, aid, title, time, topic, informant, ihome, alanguage, counter, type, images, chainid FROM veryzoo_stories ORDER BY sid DESC LIMIT 0,50
+then do while loop in each records for find the maching name of topic in each post:
while ( .. ) {
SELECT topicname FROM veryzoo_topics WHERE topicid='$topic'"
....
}
+Now
I going to use Inner Join for speed up process but as my test it took much longer from 1.5s up to 3.5s
SELECT a.sid, a.aid, a.title, a.time, a.topic, a.informant, a.ihome, a.alanguage, a.counter, a.type, a.images, a.chainid, t.topicname FROM veryzoo_stories a INNER JOIN veryzoo_topics t ON a.topic = t.topicid ORDER BY sid DESC LIMIT 0,50
It look like the inner join do all joining 200k records from two table fist then limit result at 50 .. that took long time..
Please help to point me right way doing this..
eg take last 50 records from table one.. then join it to table 2 .. ect
Do not use inner join unless the two tables share the same primary key, or you'll get duplicate values (and of course a slower query).
Please try this :
SELECT *
FROM (
SELECT a.sid, a.aid, a.title, a.time, a.topic, a.informant, a.ihome, a.alanguage, a.counter, a.type, a.images, a.chainid
FROM veryzoo_stories a
ORDER BY sid DESC
LIMIT 0 , 50
)b
INNER JOIN veryzoo_topics t ON b.topic = t.topicid
I made a small test and it seems to be faster. It uses a subquery (nested query) to first select the 50 records and then join.
Also make sure that veryzoo_stories.sid, veryzoo_stories.topic and veryzoo_topics.topicid are indexes (and that the relation exists if you use InnoDB). It should improve the performance.
Now it leaves the problem of the ORDER BY LIMIT. It is heavy because it orders the 200,000 records before selecting. I guess it's necessary. The indexes are very important when using ORDER BY.
Here is an article on the problem : ORDER BY … LIMIT Performance Optimization
I'm just give test to nested query + inner join and suprised that performace increase much: it now took only 0.22s . Here is my query:
SELECT a.*, t.topicname
FROM (SELECT sid, aid, title, TIME, topic, informant, ihome, alanguage, counter, TYPE, images, chainid
FROM veryzoo_stories
ORDER BY sid DESC
LIMIT 0, 50) a
INNER JOIN veryzoo_topics t ON a.topic = t.topicid
if no more solution come up , i may use this one .. thanks for anyone look at this post