Is it possible to get comma separated value of first n (say 10 rows of a column) rows using Mysql?
I have a query to get data greater than CURDATE(). And it will return more than 100 rows of result. What I want is, GROUP_CONCAT the first 10 rows of result.
This is my query:
SELECT GROUP_CONCAT(user_id) AS userids
FROM user_tasks
WHERE due_date > CURDATE() LIMIT 10;
am getting entire rows. I need first 10 rows only
Thanks
Use subquery:
SELECT
GROUP_CONCAT(user_id) AS userids
FROM
(SELECT
user_id
FROM
user_tasks
WHERE due_date > CURDATE()
LIMIT 10) AS users
You need to use a sub query to impose the limit, like this:
SELECT GROUP_CONCAT(sub_query.user_id) AS userids
FROM
(
SELECT user_id
FROM user_tasks
WHERE due_date > CURDATE()
LIMIT 10
) sub_query
Related
I want to select one row from the latest 30 rows based on the date.
And with the below it is selecting the random row from all the rows and thats not what I want.
SELECT * FROM product where userId=1 ORDER BY RAND() LIMIT 1
So how can I select from the latest 30? Thanks.
A sub-select will return the last 30, and then you can choose a random row from there
SELECT pr.* FROM
( SELECT * from product
WHERE pr.userId=1
ORDER BY created_at DESC
LIMIT 30 ) AS pr
ORDER BY RAND()
LIMIT 1
I have 10,000 users on registrations table and want to limit this query to 3500 users from my application. But when I investigate the logs , sometimes it counts more than 3500. I can not understand why that query returns more than limit:
select count(*)
from registrations
where (selectedtime IS NULL
AND expirationtime < NOW()
)
LIMIT 3500;
I tried manually on DB and saw sometimes more than 3500
Your query only returns 1 row, which is less than 3500.
If you want to limit the number of rows that are being counted, you need to put that in a subquery.
SELECT COUNT(*)
FROM (
SELECT 1
FROM registrations
WHERE selectedtime IS NULL AND expirationtime < NOW()
LIMIT 3500) AS x
A SELECT statement with COUNT returns the number of rows retrieved by the SELECT statement.
For performance reasons, the desired result is to limit that count.
Including a LIMIT clause in the SELECT statement will not work since it only restricts the number of rows returned, which is always one.
The solution, what I call “Limited-Count”, is done by limiting a non-count SELECT statement and wrapping it in COUNT(*).
For example:
If your count statement looks like
select count(*) from registrations where (selectedtime IS NULL AND expirationtime < NOW()) LIMIT 3500;
You can limit the results by replacing the query into:
SELECT COUNT(*) AS total
FROM (
SELECT 1
FROM registrations
WHERE selectedtime IS NULL AND expirationtime < NOW()
LIMIT 3500) AS x
If you need to know how many rows your non-count SELECT statement would have returned without the LIMIT, you could use the information function, FOUND_ROWS(). It will fetch the total rows number without running the statement again.
This does what you thought you were doing:
select count(*)
from registrations
where (selectedtime IS NULL
AND expirationtime < NOW()
)
LIMIT ROWS EXAMINED 3500;
(Available since MariaDB 5.5.21 - see https://mariadb.com/kb/en/library/limit-rows-examined/ )
I need a MYSQL query to select only latest 10 records order by insertDate, but I can't fix limit 10 because maybe some record have had same date and I want all column data in latest 10 distinct insertDate rows.
Use a sub-query to find 10'th latest insertDate:
select * from tablename
where insertDate >= (select DISTINCT insertDate from tablename
order by insertDate desc limit 9,1)
order by insertDate desc
I'm not sure if you want that DISTINCT in the sub-select or not. (I guess not...)
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
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