I have table with dates and int value (viewCount), I need to get last 20 rows by date and than sort these rows by viewCount value.
I ended with this but its not ordering by viewCount.
SELECT *
FROM `videos`
ORDER BY `videos`.`date` DESC, `videos`.`viewCount` DESC
limit 20
Thanks with help!
You can use a subquery:
SELECT v.*
FROM (SELECT v.*
FROM videos v
ORDER BY v.date DESC
LIMIT 20
) v
ORDER BY v.viewCount DESC ;
Related
I want to get the most recent 100 events that happened. The events are scattered across multiple tables. Here is an example:
SELECT * FROM log_items_purchased ORDER BY timestamp DESC LIMIT 100
UNION
SELECT * FROM log_items_fulfilled ORDER BY timestamp DESC LIMIT 100
UNION
SELECT * FROM log_items_shipped ORDER BY timestamp DESC LIMIT 100
This will return up to 300 records. I would then take the result set, order by timestamp, and take the first 100 records. How can I perform this in a single SQL query, where SQL will only return 100 records in the result set.
I realize that it would be possible to do this by removing the LIMIT 100 from each query, and then make an outer query that adds LIMIT 100, but these tables are really big, and that's really inefficient.
Put it in a subquery, then use LIMIT 100 in the main query.
SELECT *
FROM (
SELECT * FROM log_items_purchased ORDER BY timestamp DESC LIMIT 100
UNION
SELECT * FROM log_items_fulfilled ORDER BY timestamp DESC LIMIT 100
UNION
SELECT * FROM log_items_shipped ORDER BY timestamp DESC LIMIT 100
) AS x
ORDER BY timestamp DESC
LIMIT 100
If you want to do this in SQL, use a subquery:
SELECT e.*
FROM ((SELECT * FROM log_items_purchased ORDER BY timestamp DESC LIMIT 100
) UNION ALL
(SELECT * FROM log_items_fulfilled ORDER BY timestamp DESC LIMIT 100
) UNION ALL
(SELECT * FROM log_items_shipped ORDER BY timestamp DESC LIMIT 100
)
) e
ORDER BY timestamp DESC
LIMIT 100;
Note: Do not use UNION. It incurs overhead to remove duplicates.
I think this will work
(SELECT * FROM log_items_purchased ORDER BY timestamp DESC LIMIT 100)
UNION
(SELECT * FROM log_items_fulfilled ORDER BY timestamp DESC LIMIT 100)
UNION
(SELECT * FROM log_items_shipped ORDER BY timestamp DESC LIMIT 100)
ORDER BY timestamp DESC LIMIT 100
I have read count only returns one row, however if you use group by you can get multiple rows returned as such
The code that returned the result above is
SELECT event_id, COUNT( event_id ) AS nr FROM picks GROUP BY
event_id DESC LIMIT 0 , 30
However I now need to find a way to order it by DESC order. I have tried order by but then I only get 1 row back and it looks like I cant use DESC with group by. Looking for advice
UPDATE
The following give me the result as displayed in image below
SELECT event_id, COUNT( event_id ) AS nr
FROM picks
GROUP BY event_id
Order by event_id DESC
LIMIT 0 , 30
Descending Order
SELECT event_id, COUNT( event_id ) AS nr
FROM picks
GROUP BY event_id
Order by event_id DESC
LIMIT 0 , 30
Ascending Order //This is the default sort order
SELECT event_id, COUNT( event_id ) AS nr
FROM picks
GROUP BY event_id
Order by event_id
LIMIT 0 , 30
i want to select the data in a table such that it should group it by userid except one value in that column and order by date time and desc.
The problem i am getting is the grouped items are not ordering by date and time and in desc manner.
I mean the grouped item is showing earlier row.
How can i do that.
This is what i have done.
SELECT * FROM `tbljobs`
GROUP BY user_id
UNION ALL
SELECT * FROM tbljobs
WHERE user_id = '1'
ORDER BY date_time DESC
LIMIT 20"
where '1' is should not be grouped.
Your ORDER BY is only executed on the second statement. You have to use braces to order the whole results:
(SELECT *
FROM `tbljobs`
GROUP BY user_id)
UNION ALL
(SELECT *
FROM tbljobs
WHERE user_id = '1')
ORDER BY date_time DESC
Thank you friends for your suggestions. Finally I created the solution by myself after a lot of effort.
(
SELECT *
FROM (
SELECT *
FROM tbljobs
ORDER BY date_time desc
) AS A
WHERE user_id <> '1' group by user_id
)
UNION ALL
(
SELECT *
FROM tbljobs
WHERE user_id=1
)
ORDER BY date_time DESC
I want to show the last 10 lines of a table ordered by date added ascending.
I know I can select count(*) as total from tableName and $offset = 10 - total then select * from tableName order by dateadded asc limit 10 offset $offset
NOTE: I need the results to be displayed in reverse, oldest at the top, newest at the bottom therefore simply ordering by desc does not produce what i need
Can this be done in one query?
Just order DESC...
SELECT *
FROM tableName
order by dateadded DESC
Limit 10
To swap the order of the results
SELECT *
FROM (
SELECT *
FROM tableName
order by dateadded DESC
Limit 10
) r
ORDER BY dateadded
I have this table:
ID(INT) DATE(DATETIME)
Under the DATE column there are a lot of different dates, and I want to figure out the most common hour between all the rows of the table, regardless of the day.
How can I do that with a MySQL query?
SELECT HOUR(date) AS hr, COUNT(*) AS cnt
FROM yourtable
GROUP BY hr
ORDER BY cnt DESC
LIMIT 1
relevant docs: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_hour
Try this -
SELECT HOUR(`DATE`) AS `hour`, COUNT(*)
FROM `table`
GROUP BY `hour`
You could do a query like:
SELECT COUNT(daterow) AS occurrences FROM table GROUP BY daterow ORDER BY occurrences DESC LIMIT 1;
SELECT COUNT( id ) , HOUR( date )
FROM test
GROUP BY HOUR( date )
ORDER BY COUNT( id ) DESC
LIMIT 1