So i have a whole load of votes going into a voting system. I want to display how many votes i have in any one day. But i also want to then, display the amount of votes per day and spit out which day they were voted on, i.e 24k votes on 05/06/12, 27k votes on 06/06/12
SELECT count(*) AS count
FROM results
GROUP BY DAY(datesubmitted), YEAR(datesubmitted), MONTH(datesubmitted)
ORDER BY DAY(datesubmitted) DESC, YEAR(datesubmitted) DESC, MONTH(datesubmitted) DESC
Is my query, i tried to add something like
DAY(FROM_UNIXTIME(datesubmitted)) as order_day
but this just throws a null which i found interesting as i'd expect the query to fail as there aren't any outers.
Why don't you simply GROUP BY datesubmitted DESC? Also, no need to ORDER BY if it's following the same criteria as GROUP BY.
Number of votes on a specific day:
SELECT COUNT(*) AS total
FROM results
WHERE datesubmitted BETWEEN #dateMin AND #dateMax
Number of votes for each separate day:
SELECT COUNT(*) AS total, DATE(datesubmitted) AS day
FROM results
GROUP BY DATE(datesubmitted)
ORDER BY DATE(datesubmitted) DESC
UPDATED AGAIN
So just saw a new answer but for me how i got it working was:
SELECT count(*) AS count, DAY(datesubmitted) AS newday,
YEAR(datesubmitted) as newyear ,MONTH(datesubmitted) as newmonth
FROM results
GROUP BY DAY(datesubmitted), YEAR(datesubmitted), MONTH(datesubmitted)
ORDER BY YEAR(datesubmitted) DESC, MONTH(datesubmitted) DESC, DAY(datesubmitted) DESC
This way i get the correct ordering with year, month and day properly and also it displays the dates. I could concat them, but thats for another day.
Related
I have been struggling with this problem for about month..
Have been searching and reading many posts, but still can't figure out, how to make this work..
Basically: I got 2 database tables fun_posts and fun_post_upvotes And I want to
SELECT *
FROM fun_posts
ORDER BY (HOTTEST POSTS(MOST UPVOTED THIS WEEK))
This is my latest code, that won't work
SELECT *
FROM fun_posts
ORDER BY (SELECT count(*), image_id, date
FROM fun_post_upvotes
GROUP BY image_id
ORDER BY DATE(date) > (NOW() - INTERVAL 7 DAY) DESC,
count(*) DESC,
date DESC)
If I divide this line into 2 different SELECT functions, they work. I can select simple posts and I can select upvotes count ordered like I want.
But If I make them into one line like that, I get following error:
#1241 - Operand should contain 1 column(s)
EDIT NR 1:
fun_posts table
fun_post_upvotes table
Problem with Answer that I checked:
Here, look how posts are ordered in my select function. (It selects like I want) 10->134->132->2->13
And here with given code (It selects image, but not in that order) 10->122->39->8->110
You can use a join to do this
SELECT fp.*, fpu.`cnt`
FROM fun_posts fp
LEFT JOIN ( SELECT image_id, COUNT(*) AS cnt
FROM fun_post_upvotes
WHERE `date` > (NOW() - INTERVAL 7 day)
GROUP BY image_id
) fpu ON ( fpu.image_id = fp.id )
ORDER BY fpu.cnt DESC, fp.`date` DESC, fp.`id` DESC;
It selects a list from fun_post_upvotes grouped by image_id and counts the amount of rows. That list is returned to the main query and matches (LEFT JOIN) on fp.id. The query will first show the item with the most upvotes in the past 7 days, than the least. If no upvotes are found, the result will still return them, but at the bottom in no specific order.
You can edit the order by, to obtain the items in the order you like.
Here a sqlfiddle.com
I have a table in my database which is updated randomly. I'm trying to pull entries by the latest date. This part is simply and I can do it with ease. However, I want to pull the two latest dates.
Example; If my last update was 2015-06-22 and the one before than was 2015-06-12 and the one before then was 2015-06-02. I would want to pull 2015-06-22 and 2015-06-15.
I would use a LIMIT 2, however, there are an unknown amount of items that may have the same date attached.
I haven't tried anything other than the LIMIT 2. After some research, I wasn't able to find anything to reference.
Update
I used SELECT DISTINCT to get the desired results.
SELECT DISTINCT dates FROM table ORDER BY dates DESC LIMIT 2
Will give you the latest 2 dates in the table.
I would have a column set to id, that is auto incremented, and do my query like this:
SELECT * FROM tbl_name ORDER BY `id` DESC LIMIT 2
Crap McAdam you beat me to it!
You can get the latest two dates using LIMIT, like you mentioned:
SELECT latestDates
FROM myTable
ORDER BY dateColumn DESC
LIMIT 2;
And you can join that to your original table to only select rows that occur on those two dates:
SELECT m.*
FROM myTable m
JOIN(
SELECT latestDates
FROM myTable
ORDER BY dateColumn DESC
LIMIT 2) tmp ON tmp.latestDates = m.dateColumn;
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.
In my order history, I am currently listing all the customers previous orders, however, not in date order.
My current query is :
SELECT OrderItemID, Date
FROM orderitems
WHERE OrderID=$orderid
GROUP BY OrderID;
After this query, I use a while loop, for each order found, to print the past order in a table.
However I want to print past orders by 'ORDER BY Date'. After the GROUPBY I put ORDER BY Date Desc, but had no difference in results printing.
Any guidance would be appreciated.
Since you are filtering the records with OrderID, why do you need to GROUP BY it?
SELECT OrderItemID, Date
FROM orderitems
WHERE OrderID=$orderid
ORDER BY Date DESC
I'm not sure why OrderItems would have the date of the order. Based on the text, I suspect you want something like this:
SELECT OrderID, Date
FROM Orders o
WHERE CustomerId = $CustomerId
ORDER BY Date DESC;
This is mysql table called matches. It contain matches that user bets on, date when they published it and result (win,lost etc..).
With this 1st query i display on user profile his bets grouped by months
SELECT COUNT(*),date,SUM(stake), AVG(stake),result
FROM matches
WHERE u_id='$id11'
GROUP BY DATE_FORMAT(date, '%Y/%m')
ORDER BY DATE_FORMAT(date, '%Y/%m') DESC
LIMIT $startrow,10
Now what i need to do is to display how much of these bets were won , how much were lost , as u can see in the photo below
What is the best way to do it ?
I already tryed to add another query witch looks like this
SELECT COUNT(*)
FROM matches WHERE u_id='$id11' AND result='win'
GROUP BY DATE_FORMAT(date, '%Y/%m')
ORDER BY DATE_FORMAT(date, '%Y/%m') DESC LIMIT $startrow,10
and same query for every result ,but the problem is lats say there are no lost bets in this month its not returning anything for this month and than table looks in wrong order.
How about to get all win/lost in one query,your where filter is filtering the results therefore you don't get the results for the query to get lost matches
SELECT
SUM(result='win') `win_count`,
SUM(result='lost') `lost_count`
FROM matches
WHERE u_id='$id11'
GROUP BY DATE_FORMAT(date, '%Y/%m')
ORDER BY DATE_FORMAT(date, '%Y/%m')
DESC LIMIT $startrow,10
In above query i have used SUM function with an expression,when expression is used in SUM it will result in a boolean and gives 1 for expression is true and 0 if expression results as false,in other words you can simply get the count through sum function by using your conditions