I am not sure if this is possible or not
I have a table with orders and userid
I need to do a SELECT query with ORDER BY the least number of orders of a customer
Example:
userid1 has 10 orders
userid2 has 5 orders
Then SELECT should show userid2 at the top
Thanks in advance
If you have orders as ordersCount i mean as a field which keeps counts of orders then
Select * FROM YourTable order by orders ASC
Else if you have a record for each order then
Select * FROM YourTable Group by UserID order by count(*) ASC;
Apply limit like this
Select *
FROM YourTable
Group by UserID
order by count(*) ASC
limit startIndex, fetchSize;
where startIndex and fetchSize are int that define your limit
You need to group by the userid so you can count orders by user.
SELECT userid, count(1) FROM orders GROUP BY userid ORDER BY count(1) asc
You can accomplish that using GROUP BY and ordering by COUNT().
It would be something like that.
SELECT userid, COUNT(*) qt
FROM orders
GROUP BY userid
ORDER BY qt
If each order is in its own row, however, you need to aggregate them:
SELECT * from MYTABLENAME GROUP BY userid ORDER BY COUNT(*)
Count is what you're looking for
SELECT count(theField) as theCount, *
FROM theTable
GROUP BY theField
ORDER BY theCount
You can select the COUNT of a column and give it a name, then use that name to sort by. Here's an example:
SELECT
p.*,
(SELECT COUNT(o.order_id) FROM orders AS o WHERE p.partron_id=p.patron_id) AS orderCount
FROM
patrons AS p
WHERE
...
ORDER BY
orderCount ASC
UPDATE: Just realized this is only useful if you have orders in a separate table than patrons. Disregard if you are only looking at a single table.
This query expects you to have two tables, a patron table with patron information like name, and a orders table that has a patron_id column that matches the identity column in the patron table.
The advantage to doing this is that you can request patron information at the same time, so you don't need to run two queries in PHP if you're going to use that information.
Related
I am working on an app where users can take online exams and then they need to know their rank on all exams taken in the app.
This is the database table structure:
After taking the exam, I need to get the rank for the specific user (usuario) based on two rows, first row to get the rank must be the bigger (puntuacion) and in case of same (puntuacion) the second row to be taken into account is the smaller (duracion) for an specific exam subject (materia).
I need your help to create the needed query.
You want the results sorted by "puntuacion" and "duracion". That's done with order by
SELECT * FROM your_table ORDER BY puntuacion DESC, duracion ASC
It's not quite clear how you want to sort it, so you may have to switch around ASC with DESC.
If you want to add the row number and have a high enough MariaDB/Mysql-Version (Mysql 8.0) then you can use a window-function: https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html
SELECT
ROW_NUMBER() OVER (ORDER BY puntuacion DESC, duracion ASC) as row,
*
FROM your_table
ORDER BY puntuacion DESC, duracion ASC
Assuming the users are unique (delete DESC if you need to rank by ascending order) :
SELECT * FROM tableName ORDER BY puntuacion DESC, duracion DESC;
Fetch it as an array and get the row with $result[array_search($user)]. Not tested but it should work, let me know.
This is kinda heavy if you have a lot of users (it loads every users every time), but I don't see other solutions.
I have got a working query:
SELECT t.*, #rownum := #rownum + 1 AS rank FROM tb_examenes t, (SELECT #rownum := 0) r WHERE materia = 10 ORDER by puntuacion DESC, duracion ASC
Then I only have to filter for the field I need.
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
When I used the
SELECT * FROM reports WHERE staff='$username' GROUP BY taskid
on my query, I get a result of the first row from that group.
What do I need to add to get the result from the last row of that group?
last row means having id greater than the other row from that group.
I tried adding
ORDER BY id DESC
or
ORDER BY id
but it did not return the intended result.
You are using a group by function without any aggregate functions. You probably want to do an order by instead (No group by in the query):
SELECT * FROM reports WHERE staff='$username' order BY taskid desc;
Group By functions are commonly used when you want to use an aggregate function on a particular column (such as get an average row value, or a sum) and the like. If you are not using any aggregate function, then using Group By will not do anything.
If you only want to get one row from the query you can add a limit clause like this:
SELECT * FROM reports WHERE staff='$username' order BY taskid desc limit 1;
if you want the "last" row per group, then you need to specify which field defines uniqueness (i.e. what you mean by "first/last row") and then isolate those rows in a subquery.
e.g.
this gets you the max(id) for each group
SELECT taskid, max(id) as max_id FROM reports
WHERE staff ='$username'
GROUP BY taskid
and this gets the entire row(s):
select * from reports where id
in
(
SELECT max(id) as max_id FROM reports
WHERE staff='$username'
GROUP BY taskid
) x
This of course assumes that id is unique and assigned in ascending order and that therefore max(id) indicates the last row per group.
Alternatively you could rewrite this using a join:
select * from reports r
inner join
(
SELECT max(id) as max_id FROM reports
WHERE staff='$username'
GROUP BY taskid
) x
on r.id = x.max_id
Is it possible to have something like this in one query,
Count how many likes a certain ID has in image_likes and then order results from images descending by how many likes they have.
SELECT
*
FROM
`images`
ORDER BY
(SELECT COUNT(`id`) FROM `image_likes` WHERE `image_id`=images.`id`) ASC
(I have of course made up field names, but this format should work)
If possible, you might want to change the way the system works so that you can just read the total likes from a field name rather than doing a subselect.
Untested
select imageid, count(imageid) from image_likes
Group by imageid
Order by Count(imageid) desc
select * from (SELECT *,(SELECT COUNT(*) as count from image_likes il WHERE ID = i.ID)
FROM images) tbl ORDER BY COUNT
untested
I have had a look through google results, but can't find a simple answer, I have a table
id, points (and more fields, but they dont affect anything)
How would I go about Getting position of record 24(id) ordered by points DESC?
I'm not sure I've understood your question
select * from table order by points desc limit 23,1
Select Count(*) from (
Select ID from UnNamedTable A where A.points>(Select B.Points from UnNamedTable B where B.ID=24)
)
Are you trying to figure out out what place a user is in based on a points system?
If so then just find how many users have more points than a particular user then add 1.
SELECT COUNT(*) + 1 AS 'position' FROM table WHERE points >
( SELECT points FROM table WHERE id = someIDhere )
You could create a variable to keep track of your row position after sorting. Something like this would work:
select #rownum:=#rownum+1 ‘rank’, p.* from table1 p, (SELECT #rownum:=0) ID order by Points desc limit 10;