I wish to get min(somecolumn) from table from first n rows in MySQL. What is the best query to get the result?
So far I found
select min(a.column) from (select column from table limit 2000) a
select min(a.column) from table a INNER JOIN (select column from table b limit 2000) on a.pricolumn = b.pricolumn.
select min(t.columnName) from tableName as t limit 10 (Here n limit is for first 10 rows as example)
OR
select outerTable.columnName from (select distinct t.columnName from tableName as t order by t.columnName asc limit 10) as outerTable limit 1,1
Try the below syntax:
SELECT min(columname) FROM tablename limit n
Related
I have a mysql query which will return all the details from table along with i need max_row count i.e total no of rows in a table using COUNT(*) in a single select query without using cross join.
Note: MySQL version is earlier version of 8
Query :
SELECT * FROM tablename ORDER BY column name DESC LIMIT 0,10;
The total count of a table is simple, when you want to add it to every row.
SELECT
*
,(SELECT COUNT(*) FROM tablename ) count1
FROM tablename
ORDER BY column name
DESC LIMIT 0,10;,
I do know how to access last N records from the table i.e,
SELECT * FROM table_name ORDER BY auto_incremented_id DESC LIMIT N;
There are N records means and table records keep incrementing every day, each record having unique serial ID from 1 to N.
now i want to retrieve 10 records of last N-10 rows.
please consider the example
Record 1
Record 2
Record 3
Record 4
Record 5
Record 6
Record N-M
Record N-2
Record N-1
Record N
How do i can retrieve N-2 to N-M rows
The solution required for an website - 1st page displays last 10 records, 2nd Page displays last next 10 records from the reverse except rows are displayed in first page and it goes on up to 1st record of the table.
you can play with ordering and your limit conditions
SELECT * from (SELECT * from table_name ORDER BY auto_incremented_id DESC LIMIT N) as temp ORDER BY auto_incremented_id ASC LIMIT M
assuming as you say a paging size of 10
first page:
SELECT * from as temp ORDER BY auto_incremented_id DESC LIMIT 10;
second page:
SELECT * from (SELECT * from table_name ORDER BY auto_incremented_id DESC LIMIT 20) as temp ORDER BY auto_incremented_id ASC LIMIT 10;
third page:
SELECT * from (SELECT * from table_name ORDER BY auto_incremented_id DESC LIMIT 30) as temp ORDER BY auto_incremented_id ASC LIMIT 10;
For dynamically limiting the output rows you can use inline views (as MySQL doesn't support common table expressions using WITH clause). An example code is as follows:
SELECT *
FROM (SELECT id, name
, #i := #i + 1 as result
FROM table_name
, (select #i := 0) temp
ORDER BY id) v
CROSS JOIN (SELECT COUNT(id) AS M FROM table_name) w
WHERE result BETWEEN w.m-2 AND w.m;
Fiddle.
I have the following table (user_record) with millions of rows like this:
no uid s
================
1 a 999
2 b 899
3 c 1234
4 a 1322
5 b 933
-----------------
The uid can be duplicate .What I need is to show the top ten records(need inclued uid and s) with no duplicate uid order by s (desc). I can do this by two steps in the following SQL statements:
SELECT distinct(uid) FROM user_record ORDER BY s DESC LIMIT 10
SELECT uid,s FROM user_record WHERE uid IN(Just Results)
I just wana know is there a bit more efficient way in one statement?
Any help is greatly appreciated.
ps:I also have following the SQL statement:
select * from(select uid,s from user_record order by s desc) as tb group by tb.uid order by tb.s desc limit 10
but it's slow
The simpliest would be by using MAX() to get the highest s for every uid and sorted it based on the highest s.
SELECT uid, MAX(s) max_s
FROM TableName
GROUP BY uid
ORDER BY max_s DESC
LIMIT 10
SQLFiddle Demo
The disadvantage of the query above is that it doesn't handles duplicates if for instance there are multiple uid that have the same s and turn out to be the highest value. If you want to get the highest value s with duplicate, you can do by calculating it on the subquery and joining the result on the original table.
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT DISTINCT s
FROM TableName
ORDER BY s DESC
LIMIT 10
) b ON a.s = b.s
ORDER BY s DESC
Lets say I have a 1000 rows in my table.
I want to select 10 of those at random.
SELECT * FROM table ORDER BY RAND() LIMIT 10
Then I want to select the row in that result with the highest value for number
SELECT * FROM table ORDER BY number DESC LIMIT 1
Can anyone help me come up with an efficient way of doing this?
Just use a subquery:
SELECT *
FROM (
SELECT * FROM table ORDER BY RAND() LIMIT 10
)
ORDER BY number DESC LIMIT 1
I have a column in mysql table that has the the data type INT(11).
How can I search to get the top 10 most occurring values in this column?
SELECT col, count(*)
FROM tablethingie
GROUP BY col
ORDER BY count(*) DESC
LIMIT 10
Try the following code
SELECT colname, COUNT(*) AS cnt
FROM tablename
GROUP BY colname
ORDER BY cnt DESC
LIMIT 10
TOP is a keyword which is not supported in MySQL, it is in MSSQL though.
This following query should do what you want (untested, but the idea should become clear):
SELECT column, COUNT(*) AS matches
FROM table
GROUP BY column
ORDER BY matches DESC
LIMIT 10
Try:
SELECT ColName, Count(1) AS occurances
FROM
table
GROUP BY
ColName
ORDER BY
occurances DESC
LIMIT
10