I have a SQL query in mySQL that generates the difference between two columns that's limited to 100 entries and sorted by another column. It gives me a list of 100 values, and I want to be able to find the average of the generated answers from the query; I know how to find the AVG of all the differences, but I don't need that value.
My Query:
SELECT ABS(VAL1-VAL2)
FROM USER.TABLE1
ORDER BY JOB DESC
LIMIT 100;
What I want to get is how to find the average of this query's results
You would do it as follows:
SELECT AVG(t.result)
FROM (SELECT ABS(VAL1-VAL2) AS result
FROM USER.TABLE1
ORDER BY JOB DESC
LIMIT 100) t;
Related
Today while I was writing a complex query, accidently I found that even I have set LIMIT in my query MySQL server returns the total number of rows for COUNT.
Example:
SELECT COUNT(*) FROM `log` LIMIT 10;
Output:
5219
But if I run the query without COUNT it returns only 10 rows. My question is,
Why does MySQL ignore LIMIT when the COUNT is present?
LIMIT is for returning a subset of the total results, in your case the result is only one line so no effect
The purpose of LIMIT clause to select a limited number of records whereas count is aggregate function, which will return result of aggregation. It won't make any sense to use LIMIT and COUNT together as LIMIT may return any random n records.
Is there a way that I can get the COUNT(*) of what a query will return? For example:
SELECT * FROM table LIMIT 10 // Query
SELECT (*) FROM table LIMIT 10 // Query Count
This would actually ignore the limit (
MySQL COUNT with LIMIT). While this might be fine and 'correct' within sql, I need the exact number of rows the query is returning. How would this be done?
if you actually want to accommodate limit, you can use:
select count(*) from (SELECT * FROM table LIMIT 10) as t
Currently I'm working with a database on phpmyadmin. I'm trying to find the Average of an SQL statement that is implementing a LIMIT code.
SQL Statement -
SELECT avg(value) FROM que LIMIT 10
The problem with the code is its not averaging the first 10 numbers in value column, but all of them. So the LIMIT 10 isn't actually working. Is there anyway to avoid this or an alternative?
You need to use a subquery:
SELECT avg(value)
FROM (select value
from que
LIMIT 10
) q;
Do note, however, that use of limit without an order by produces arbitrary results -- there is no definition of the "first ten" records in a table.
Hey Guyz Is this possible to get total number of rows count with offset limit
Scenario
SELECT * FROM users limit 0,5;
This Query contain 300 records but the issue is if i call this query with offset the result will be show only 5 record and i don't want to write a Query in twice time. one for paging limit and other for total no of record count...
I don't want this
SELECT * FROM users limit 0,5; // paging
SELECT count(*) FROM users; // count
i have to merge this Queries or helps are definitely appreciated
You can use SQL_CALC_FOUND_ROWS like this
SELECT SQL_CALC_FOUND_ROWS * FROM users limit 0,5;
It gets the row count before applying any LIMIT clause. It does need another query to fetch the results but that query can simply be
SELECT FOUND_ROWS()
and hence you don't have to repeat your complicated query.
the table videos has the folowing feels
id,average,name
how can i write the query, to select the name of video, which have the max average!!!
i can do that vith two queries, by selecting the max(avege) from the table, and then find out the name, where ihe average equal to max!!! but i want to do that in one query!!!
help me please!!!
You don't need a group by for this, you just want to select the highest average!
SELECT * FROM videos ORDER BY average DESC LIMIT 1;
You can use an ORDER BY with a LIMIT:
SELECT id, average, name FROM videos ORDER BY average DESC LIMIT 1
ORDER BY average DESC orders the rows in order of descending average (i.e. the first row will have an average equal to MAX(average)). LIMIT 1 causes only the first row to be returned.
SELECT id,name,MAX(average) FROM videos;
All fields you choose to SELECT will be returned. Getting more data back is just a case of SELECTing more fields.