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
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 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
Is there any way how to order top rows in a query in a different way than the rest? For example, if I have a list of products with name and price, I would like to get the list ordered in a way that 10 most expensive products are on top ordered by price desc, and the rest below is ordered by product name.
What comes in my mind is something like this:
SELECT id,name, price FROM products ORDER BY price DESC LIMIT 10
UNION ALL
SELECT id,name,price FROM products
WHERE id NOT IN
(SELECT id FROM products ORDER BY price DESC LIMIT 10)
ORDER BY name
but this query does not execute, it prints Incorrect usage of UNION and ORDER BY, furthermore if I wrap the selects into another selects, it prints LIMIT & IN/ALL/ANY/SOME subquery. Any idea?
select * from
(
SELECT 1 a, id,name, price FROM products ORDER BY price DESC LIMIT 10
UNION ALL
SELECT 0, id,name,price FROM products
WHERE id NOT IN
(SELECT id FROM products ORDER BY price DESC LIMIT 10)
)
order by a*price desc, name
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 select last 50 rows from MySQL database within column named id which is primary key. Goal is that the rows should be sorted by id in ASC order, that’s why this query isn’t working
SELECT
*
FROM
`table`
ORDER BY id DESC
LIMIT 50;
Also it’s remarkable that rows could be manipulated (deleted) and that’s why following query isn’t working either
SELECT
*
FROM
`table`
WHERE
id > ((SELECT
MAX(id)
FROM
chat) - 50)
ORDER BY id ASC;
Question: How is it possible to retrieve last N rows from MySQL database that can be manipulated and be in ASC order ?
You can do it with a sub-query:
SELECT * FROM
(
SELECT * FROM table ORDER BY id DESC LIMIT 50
) AS sub
ORDER BY id ASC;
This will select the last 50 rows from table, and then order them in ascending order.
SELECT * FROM table ORDER BY id DESC LIMIT 50
save resources make one query, there is no need to make nested queries
SELECT * FROM table ORDER BY id DESC, datechat DESC LIMIT 50
If you have a date field that is storing the date (and time) on which the chat was sent or any field that is filled with incrementally (order by DESC) or de-incrementally (order by ASC) data per row put it as second column on which the data should be ordered.
That's what worked for me!!!! Hope it will help!!!!
Use it to retrieve last n rows from mysql
Select * from tbl order by id desc limit 10;
use limit according to N value.
if anyone need this
you can change this into
SELECT
*
FROM
`table`
WHERE
id > ((SELECT
MAX(id)
FROM
chat) - 50)
ORDER BY id ASC;
into
SELECT
*
FROM
`table`
WHERE
id > (SELECT MAX(id)- 50 FROM chat)
ORDER BY id ASC;
select * from Table ORDER BY id LIMIT 30
Notes:
* id should be unique.
* You can control the numbers of rows returned by replacing the 30 in the query