SQL order by after more than - mysql

I am trying SQL to sort results
id | name (table name: student)
-----
1 | jhon
2 | amin
3 | heli
4 | mir
5 | mrs
6 | amr
7 | jonnathan
8 | adhy
When i use this query
select id from studenth where id>='3' order by id DESC limit 2
The result that appears is
id | name (table name: student)
-----
8 | adhy
7 | jonnathan
Whereas I want to sort results after id = 3, I want the data as below
id | name (table name: student)
-----
4 | mir
3 | heli

select * from (select id from student where id >= 3 order by id limit 2) r order by r.id desc

You can use this query. You want everything less than or equal to 4 instead of everything after 3.
select id from student where id<='4' order by id DESC limit 2

You can do some like:
select * from student where id>=3 order by id LIMIT 2
If you want to take the records and sort it in descending order,then you can go for a subquery.
SELECT * FROM
( SELECT *
FROM student
WHERE id>=3 ORDER BY id LIMIT 2) X
ORDER BY X.id DESC
Problem with your Query
select * from student where id>='3' order by id DESC limit 2
The above query will take all the results with id=3 or more and order it in the descending order and with LIMIT 2 it will display only 2 records.
That's why it's displaying the last 2 records.

TRY THIS Use sub query it's easy to understand and fulfill your requirement:
SELECT id, name
FROM studenth
WHERE id IN (SELECT id
FROM studenth
WHERE id >= 3 ORDER BY id LIMIT 2) ORDER BY id DESC

If i understood correctly , you want the data in descending order from id +1 record. the query should be something like below:
select id from studenth where id<='4' order by id DESC limit 2;

Remove single quotes in this id>='3'
select id from student where id>=3 order by id desc limit 2
Update 1:
Order by desc limit 2 problem here use subquery get the result then apply descending .
select * from (select id from student where id >= 3 order by id limit 2) new order by new.id desc

Please try the following...
SELECT id,
name
FROM tblTable
WHERE id >= 3
ORDER BY id DESC
LIMIT ( ( SELECT COUNT( * )
FROM tblTable
WHERE id >= 3 ) - 1 ), 2;
This statement works by sorting the records from tblTable based on the value of id. It then uses LIMIT to select specific records from the sorted list. If there are 20 eligibnle records this equates to LIMIT 19, 2, which will give us the 19th and 20th records.
If you have any questions or comments, then please feel free to post a Comment accordingly.

Related

Sorted data in groups

Dataset:
id uid activity postid
1 20 A 1
2 20 A 1
3 6 A 1
4 3 A 1
5 6 A 1
6 13 A 1
7 13 B 1
8 18 B 1
9 18 B 1
10 1 A 1
Current Results:
id uid uid_list groupCount activity postid
9 18 18,13 3 B 1
1 20 1,13,6,3,20 7 A 1
Expected Results:
id uid uid_list groupCount activity postid
9 18 18,13 3 B 1
10 1 1,13,6,3,20 7 A 1
The query I have:
SELECT
id,
uid,
GROUP_CONCAT(DISTINCT uid ORDER BY id DESC) as uid_list,
COUNT(*) as groupCount,
activity,
postid
FROM (
SELECT *
FROM `user_activities`
ORDER BY id DESC) as tbl
GROUP BY
activity, postid
ORDER BY
id DESC
I want to group by activity and postid while having the result in descending order by id. And want to have the latest id and uid for every group. I don't understand why this query doesn't return the expected output.
From what I understand id value is increasing. To get the latest values you could use an aggregate function MAX().
Also, your inner query with ordering is unnecessary because the engine has to sort the resultset by id anyways when building result for GROUP_CONCAT().
To retrieve uid for a particular id column you need to self join to the same table.
SELECT
a.id, b.uid, a.uid_list, a.groupcount, a.activity, a.postid
FROM (
SELECT
MAX(id) as id,
GROUP_CONCAT(DISTINCT uid ORDER BY id DESC) as uid_list,
COUNT(*) as groupCount,
activity,
postid
FROM user_activities a
GROUP BY
activity, postid
) a
INNER JOIN user_activities b ON a.id = b.id
Probably the simplest method is the group_concat()/substring_index() trick:
SELECT MAX(ID) as id,
SUBSTRING_INDEX(GROUP_CONCAT(uid ORDER BY ID DESC), ',', 1) as uid,
GROUP_CONCAT(DISTINCT uid ORDER BY id DESC) as uid_list,
COUNT(*) as groupCount,
activity, postid
FROM user_activities ua
GROUP BY activity, postid
ORDER BY id DESC;
There are some limitations to this approach, in the sense that GROUP_CONCAT() has a maximum length for the intermediate value. Typically the default is sufficient, but you might need to change that value if many, many rows match each group (and you already have this issue for the list of uids anyway).

How to select the row with highest value from the result of an existing query?

I have an SQL query that returns a list of results. The sql is :
SELECT fcategory,sum(fquantity*fprice) AS qty FROM items GROUP BY fcategory
The output is something like
----------------
category|qty
----------------
a | 10
b | 100
c | 554
Based on the result of the sql statement above, how do I retrieve the row with maximum quantity? (in this case I want the query to return just "c" )
you can add order by + limit 1 to get this :
SELECT fcategory,sum(fquantity*fprice) AS qty FROM items GROUP BY fcategory
order by qty desc limit 1
In order to fetch the highest qty, just get the first result ordered by qty like:
SELECT * FROM items ORDER BY qty desc limit 1
In your own case,
SELECT fcategory,sum(fquantity*fprice) AS qty FROM items GROUP BY fcategory ORDER BY qty LIMIT 1

How to get the end of an Ascending list of records with a limit?

I'm fighting a bit with a query I'm building. Let's say I've got a DB table like this:
id | some_string
----------------
1 | 'lala'
2 | 'jeje'
3 | 'poopoo'
4 | 'wicked wicked'
I now want to get the last three records (2, 3, and 4) ordered ascending by key. I tried this:
SELECT * FROM tableName LIMIT 3 ORDER BY id ASC
This gets me the first three records, instead of the last three. I can of course also use the query below, which gets me the correct records, but then I don't get them in Ascending order:
SELECT * FROM tableName LIMIT 3 ORDER BY id DESC
Does anybody know how I can get the last three records in an ascending order? All tips are welcome!
select * from (
select * from table_name order by id desc limit 3
) last_3_rows
order by id
Sort on the resulting result set ie. do a select * from (<your query here>) order by id
This is a query inside another query. that reorders your query.(SQL - How to reorder a select query that uses the limit constraint)
select * FROM (SELECT * FROM tableName LIMIT 3 ORDER BY id DESC) AN_UNUSUAL_NAME ORDER BY id ASC

Arrange data in specific order

I have a user table that contain 8 records. I want to arrange the data in descending order on the basis of field id (that is a primary key of that table) but except id 3 and 5. So eventually the result should be like
id name
-- ----
3 peter
5 david
8 john
7 stella
6 jim
4 jack
2 nancy
1 scott
Except id 3 and 5 rest of the data should be arranged in descending order and 3 and 5 should come in ascending order.
SELECT * FROM user ORDER BY IF(id=3 OR id=5, id, ~id) ASC
something like this:
order by
case
when id = 3 then 999
when id = 5 then 998
else id
end desc
This assumes that you really don't have more than 8 rows. Otherwise you must change the "magic" numbers that move 3 and 5 to the top.
I think the trick here is to use an enum.
SELECT id, name FROM my_table WHERE id IN (3, 5) ORDER BY ASC
UNION
SELECT id, name FROM my_table WHERE id NOT IN(3, 5) ORDER BY DESC
In MySQL, there is a function called FIELD which *returns zero if a value is not found on the list` eg,
SELECT *
FROM tableName
ORDER BY FIELD(id, 5, 3) DESC, id DESC
SQLFiddle Demo
FIELD

MySQL - Select 3 highest values and sort alphabetically

I have this table with thousands of rows:
id | Keyword | hits
1 cat 3
2 cats 5
3 castle 1
4 cream 2
5 car 12
. . .
I want to select the 3 rows with most hits and then order alphabetically.
So I want to return:
car
cat
cats
I have this:
SELECT keyword,hits FROM table ORDER BY hits DESC, keyword ASC LIMIT 3
but it only orders by hits and then if hits are equal it orders alphabetically.
Wrap your SELECT - that gets only the 3 rows with highest hits - as a subquery inside another query that orders by the keyword:
SELECT * --- or just: SELECT keyword
FROM
( SELECT keyword, hits
FROM table
ORDER BY hits DESC
, keyword ASC --- this is needed only in case of ties at 3rd place
LIMIT 3
) tmp
ORDER BY keyword
You could use a subquery (don't know about your mysql version).
SELECT *
FROM
(SELECT keyword, hits
FROM table
ORDER BY hits DESC
LIMIT 0,3) AS topThree
ORDER BY keyword ASC