How to make a query that brings the results of the day - mysql

I have this query but I want to bring the results processed on the day and if there is no data in excess on the day bring the last 100 processed.
SELECT * FROM tbl_classe where classe>= '2019-10-15';

You can use order by and limit (in both Postgres and MySQL):
SELECT *
FROM tbl_classe
ORDER BY classe DESC
LIMIT 100;
I'm not sure what 2019-10-15 has to do with the query. You seem to want the 100 most recent entries.

IF (SELECT COUNT(*) FROM tbl_classe WHERE classe>= '2019-10-15' ) = 0
THEN
SELECT TOP 100 * FROM tbl_classe ORDER by classe DESC, id DESC
ELSE
SELECT * FROM tbl_classe where classe>= '2019-10-15';
END
As per David's request (code seems pretty self-documenting to me..)
Count the number of records for the chosen date.
If the count is zero then get the 100 most recent records.
If the count is > zero then return the records for the day

Related

MySQL - Order by one column for a limit then by another column for a limit

I'm developing an application that displays rows which have columns "Time created" and "Number of Likes".
I'm trying to return the first 5 results of my query with the rows that have the most likes, and then the remaining 95 by most recent date, without duplication.
My current query only accomplishes ordering by date.
"SELECT * FROM `$category` ORDER BY time DESC LIMIT 100"
Is what I'm attempting to do possible in one query? Or will I have to perform two queries and filter out duplicate rows?
UNION automatically removes duplicate rows and LIMIT outside the parethesis applies to the whole query.
(SELECT *, like_count AS likes FROM category ORDER BY like_count DESC LIMIT 5)
UNION
(SELECT *, 0 AS likes FROM category ORDER BY time DESC)
ORDER BY likes DESC, time DESC
LIMIT 100

MySQL select last 'n' records by date, but sorted oldest to newest

I have a table that has transactions with a datetime column. I'm trying to select the last 'n' records (i.e. 20 rows) but have it sorted oldest to newest.
SELECT *
FROM table
WHERE 1=1
ORDER BY table.datefield DESC
LIMIT 20;
Gives me the 20 most recent, but in the opposite order.
Is this possible in one query, or will I have to do a query to get total rows and then adjust the limit based on that so I can do the table.datefiled ASC and then limit (total rows - n), n
Building a SELECT around your original SELECT and convert this to a derived table should do it
SELECT t.*
FROM (
SELECT *
FROM table
WHERE 1=1
ORDER BY table.datefield DESC
LIMIT 20
) t
ORDER BY t.datefield

MySQL select given number of rows and always select all rows within the same day

I want to do a MySQL Query which selects a given number of Rows from a single table from a given offset like
SELECT * FROM table
WHERE timestamp < '2011-11-04 09:01:05'
ORDER BY timestamp DESC
LIMIT 100
My problem is that i always want all rows within a day if one row of a day will be included in the result.
It would be no problem to have a result with e.g. 102 rows instead of 100
Can i realize this with a single SQL statement?
Thanks for your help!
This seems to work on my system:
SELECT UserID, Created
FROM some_user
WHERE Created < '2011-11-04 09:10:11'
AND Created >= (
SELECT DATE(Created) -- note: DATE() strips out the time portion from datetime
FROM some_user
WHERE Created < '2011-11-04 09:10:11'
ORDER BY Created DESC
LIMIT 99, 1 -- note: counting starts from 0 so LIMIT 99, 1 returns 100th row
)
ORDER BY Created DESC
-- 0 rows affected, 102 rows found. Duration for 1 query: 0.047 sec.
There might be a faster alternative.
If I understand your question correctly, you're intrested in retrievieng 100 rows, + any rows that are on the same day as ones already retrieved. You can do this using a subquery:
SELECT table.*
FROM table, (
SELECT DISTINCT day
FROM (
SELECT TO_DAYS(timestamp) day
FROM table
WHERE timestamp < :?
LIMIT 100
)
) days
WHERE TO_DAYS(table.timestamp) = days.day
ORDER BY timestamp
Exclude the time part in the query and remove the LIMIT.
SELECT * FROM table
WHERE timestamp < '2011-11-04 00:00:00'
ORDER BY timestamp DESC

mySQL: Can I make count() honor limit clause?

I'm trying to get a count of records matching certain criteria within a subset of the total records. I tried (and assumed this would work)
SELECT count(*)
FROM records
WHERE status = 'ADP'
LIMIT 0,10
and I assumed this would tell me how many records of status ADP were in that set of 10 records. It doesn't - it returns, in this case 30, which is the total number of ADP records in the table.
How do I just count up the records matching my criteria including the limit?
SELECT count(*)
FROM ( SELECT records
FROM table
WHERE status = 'ADP'
LIMIT 0,10
)
select count(*) from (select * from records where status='ADP' limit 0,10) as t;

How to SUM() from an offset through the end of the table?

If SELECT SUM(amount) FROM transactions ORDER BY order LIMIT 0, 50 sums the amount field for the first 50 records in a table, how do a sum all records after the first 50? In other words, I'd like to do something like SELECT SUM(amount) from transactions ORDER BY order LIMIT 50, *, but that doesn't work.
SELECT SUM(amount)
FROM (
SELECT amount
FROM transactions
ORDER BY
order
LIMIT 50, 1000000000000
) q
Note that your original query:
SELECT SUM(amount)
FROM transactions
ORDER BY
order
LIMIT 0, 50
does not do what you probably think it does. It is synonymous to this:
SELECT a_sum, order
FROM (
SELECT SUM(amount) AS a_sum, order
FROM transactions
) q
ORDER BY
order
LIMIT 0, 50
The inner query (which would normally fail in any other engine but works in MySQL due to its GROUP BY extension syntax) returns only 1 records.
ORDER BY and LIMIT are then applied to that one aggregated record, not to the records of transactions.
The documentation advices to use an incredible large number as second parameter to LIMIT:
To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:
SELECT * FROM tbl LIMIT 95,18446744073709551615;
There is probably a more efficient way, but you could run a count query first, to retrieve total # of rows in your table:
SELECT count(*) FROM transactions
Stuff that into a variable and use that variable as your second argument for LIMIT. You could probably do this as a nested mysql query.