Count total rows ignoring the select limit - mysql

I want to make a select in MySQL having limit of rows, but in the same select to count the total number of rows (not between limits).
How can I alter the code bellow to count all rows from that products table? If I add a count in this select it will count only 10.
SELECT id,
name,
categ
FROM products
LIMIT 0, 10

you can try this :
SELECT id,name,categ ,( SELECT count(*)
FROM products) as numberRows FROM products
LIMIT 10
there is an example :
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=ccc90496f13be1cc046b72cd9078c080

Related

want to get total count of records in a table using COUNT(*) in MySQL

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;,

mysql select highest and lowest values with limit in one statement

I need to pull the 5 highest prices and 5 lowest prices from a table products on column prices. I thought I could do two select in one stmt like below, but I think you cannot because it is the same table? I have done similar stmts and it worked but with different tables.
SELECT products.* AS fullcount, (SELECT * FROM products ORDER BY price ASC LIMIT 5) AS highest, (SELECT * FROM products ORDER BY price DESC LIMIT 5) AS lowest FROM products
What am I doing wrong or should I be using a different approach?
Use UNION to combine the results of queries that get the highest and lowest rows.
SELECT *
FROM (
SELECT *
FROM products
ORDER BY price DESC
LIMIT 5) x
UNION (
SELECT *
FROM products
ORDER BY price ASC
LIMIT 5
) y

Mysql How can i find max and min after sum a column

This is my code from tables
i want to select from it the first two and the last two rows as a result. is that possible?
in other words i want to select min and max of a column after it was summed. prefer two of each if possible
thanks a lot
`select A.prod_id, SUM(quantity) Total
from charging A
group by prod_id
order by total`
You can use a subquery. Because you didn't bother to include your query in the question, I am not going to re-type it. For just the values:
select min(Total), max(Total)
from (<your query here>) s
For four rows with both values:
(select t.*
from (<your query here>) t
order by Total asc
limit 2
) union all
(select t.*
from (<your query here>) t
order by Total desc
limit 2
)

How can I limit query's results without using LIMIT

I need to show ordered 20 records on my grid but I can't use LIMIT because of my generator(Scriptcase) using LIMIT to show lines per page. It's generator's bug but I need to solve it for my project. So is it possible to show 20 ordered record from my table with a query?
As from comments,if you can't use limit then you can rank your results on basis of some order and in parent select filter limit the results by rank number
select * from (
select *
,#r:=#r + 1 as row_num
from your_table_name
cross join (select #r:=0)t
order by some_column asc /* or desc*/
) t1
where row_num <= 20
Demo with rank no.
Another hackish way would be using group_concat() with order by to get the list of ids ordered on asc/desc and substring_index to pick the desired ids like you need 20 records then join with same table using find_in_set ,But this solution will be very expensive in terms of performance and group_concat limitations if you need more than 20 records
select t.*
from your_table_name t
join (
select
substring_index(group_concat(id order by some_column asc),',',20) ids_list
from your_table_name
) t1 on (find_in_set(t.id , t1.ids_list) > 0)
Demo without rank
What about SELECT in SELECT:
SELECT *
FROM (
-- there put your query
-- with LIMIT 20
) q
So outer SELECT is without LIMIT and your generator can add own.
In a Scriptcase Grid, you CAN use Limit. This is a valid SQL query that selects only the first 20 records from a table. The grid is set to show only 10 records per page, so it will show 20 results split in a total of 2 pages:
SELECT
ProductID,
ProductName
FROM
Products
LIMIT 20
Also the embraced query works out well:
SELECT
ProductID,
ProductName
FROM
(SELECT
ProductID,
ProductName
FROM Products LIMIT 20) tmp

MySQL select random rows with order by

I want to retrieve random rows from table but this rows must be order in category.
select category,
(select order_number
from orders
where order_number in (123,125,128,129,256,263,966,258,264,159,786)
order by rand())
from orders
order by category
This is the query I tried. But that retrieves whole data in table.
Worked query ;
SELECT category,order_number FROM (
SELECT category,order_number
from orders
where order_number in (`$order_numbers_variable`)
order by rand()
) order by category
I assume the requirement is:
Retrieve 'N' random rows from a table sorted by 'category'.
Lets assume N is 10. If you want to change the number of rows, then change it in the LIMIT clause.
SELECT * FROM (
SELECT category from orders ORDER BY rand() ASC LIMIT 10
) AS innerResult
ORDER BY innerResult.category