I have a table like this:
id | name | class | marks
1 | abc | 1 | 90
2 | cdf | 1 | 100
3 | xyz | 1 | 70
I want to get 2nd highest marks record. How can I get it with with one query. Simple and short?
SELECT * FROM `tableName` ORDER BY `marks` DESC LIMIT 1,1
Use LIMIT and ORDER
SELECT * FROM table
ORDER BY marks DESC LIMIT 1,1
ORDER BY marks DESC means: descending ordering, so highest on top.
LIMIT 1,1 means offset = 1 and only select 1 row.
Related
I have table named posts with 3 columns which are id, details, date which contains the following data in ascending order:
+----+----------+-------+
| id | details | date |
+----+----------+-------+
| 1 | details1 | date1 |
| 2 | details2 | date2 |
| 3 | details3 | date3 |
| 4 | details4 | date4 |
+----+----------+-------+
I want to select data in descending order but I want to leave the first row details, like I want to leave row 4th id details, details4, date4, but then I want to select data from id 3 to 2 in, like order by id desc limit 2 but leave the first row from last
You can use the query with ORDER BY DESC LIMIT 1, n
That way, n is the amount of rows you want to fetch, and you're skipping the first row of the result by using LIMIT 1, .
WITH a AS (
SELECT 1 i
UNION ALL
SELECT 2 i
UNION ALL
SELECT 3 i
UNION ALL
SELECT 4 i
)
, b as (
SELECT TOP 1 i FROM a ORDER BY i DESC
)
SELECT *
FROM A
EXCEPT
SELECT * FROM B
ORDER BY i DESC
Example
id |u_id
1 | 1
2 | 1
3 | 2
4 | 1
5 | 2
6 | 3
I know, the id 4 has u_id of 1, but I want to select the last row having u_id 1 before that with id 4 i.e. I want to select the row with the id 2.
Note that I don't know this id.
How can I achieve that?
This is what the result should look like:
id |u_id
2 | 1
4 | 1
select * from table where uid=1 order by id desc limit 2
This may help you.
SELECT * FROM ( SELECT * FROM yourTable WHERE u_id=1 ORDER BY id DESC LIMIT 2) AS tab ORDER BY tab.id ASC
Finally figured out the correct sql query for it.
SELECT * FROM table WHERE u_id = 1 AND (id = 4 OR id < 4) ORDER BY id DESC LIMIT 0,2
If you are using a fairly recent version of MySQL, what you need is the LAG() windowed function:
SELECT id,
u_id,
LAG(id) OVER W AS prev_id
FROM MyTable
WINDOW w AS (PARTITION BY u_id ORDER BY id)
ORDER BY id, u_id;
It will produce a result like this:
id |u_id |prev_id
1 | 1 | null
2 | 1 | 1
3 | 2 | null
4 | 1 | 2
5 | 2 | 3
6 | 3 | null
You can play with the query here.
As your title states:
select previous record from row
The following gives you every row that comes prior to a 2, and will work with more than just the data you've shown in your example:
SELECT *
FROM example
WHERE id IN (
SELECT id - 1
FROM example
WHERE u_id = 2);
[SEE DEMO HERE]
So I'm currently using the following bit of SQL to select the closest rank value to the given variable but I'm looking to implement a feature so I can grab the closest rank value but nothing greater than the variable.
Here is my current SQL statement:
SELECT rank, points
FROM `4star`
WHERE arenaID = 6
ORDER BY ABS(rank - $v) ASC
LIMIT 1
$v indicates the PHP variable.
If this was my table:
+---------+----------+
| rank | points |
+---------+----------+
| 1 | 9 |
| 50 | 7 |
| 200 | 6 |
| 5000 | 4 |
| 10000 | 1 |
+---------+----------+
how would I select the closest rank to 3000 that was not greater than 3000? So the row I would get would be 200 => 6?
Try this:
SELECT rank,points
FROM `4star`
WHERE rank <=3000
AND arenaID = 6
ORDER BY rank Desc
LIMIT 1
how would I select the closest rank to 3000 that was not greater than
3000?
Use WHERE to select rows where rank is less than/equal to 3000, then ORDER BY rank descending and LIMIT the results to one row:
SELECT rank
FROM table
WHERE rank <= 3000 AND arenaID = 6
ORDER BY rank DESC
LIMIT 1
I have a leader board of high scores and need to not show the duplicate records for a more accurate leader board.
Table: highscores
+-------------------+----------------+-------------+-------+
| id | name | time | moves | score |
+-------------------+----------------+-------------+-------+
| 1 | person1 | 33 | 22 | 245 |
+-------------------+----------------+-------------+-------+
| 2 | person1 | 83 | 31 | 186 |
+-------------------+----------------+-------------+-------+
and my query is
SELECT * FROM highscores ORDER by Score DESC LIMIT 100
how can I change the query to only show the higherscore of duplicate records without messing up the descending part
this seems to be working
SELECT * FROM highscores GROUP BY name ORDER by Score DESC LIMIT 100
Use mysql's custom group by:
SELECT * FROM (
SELECT * FROM highscores
ORDER by Score DESC) x
GROUP BY name
ORDER by Score DESC
LIMIT 100
This works because when not all non-aggregate columns are listed in the group by, mysql returns the first row encountered for each unique combination of the columns listed in the group by.
I have a student_table table with two columns student_name(with uniqe constraint),student_marks. From this table, I need to get the record of the student who has the 3rd highest marks.
I tried this, but it's incorrect:
select * from student_table orderby(marks) enum(marks)=3;
How do I correct this query?
Here is a very simple solution
select *
from marks
order by marks desc
limit 2,1
with limit you can use offset and length. Here offset is set to two because it starts from 0. For third record. And 1 is the limit.
Here is another solution
$res=mysql_query("SELECT * from marks order by marks desc");
mysql_data_seek($res, 2);
$row=mysql_fetch_assoc($res));
Try this
SELECT * FROM `student_table` ORDER BY marks DESC LIMIT 2,1
If you just want the 3rd record:
SELECT * FROM student_table ORDER BY marks DESC LIMIT 2,1
Have a read about the LIMIT command.
In MySQL you can do:
SELECT * FROM Student_Table ORDER BY Marks LIMIT 2, 1
select * from students_table orderby marks limit 2,1
Check this url to more about limit http://php.about.com/od/mysqlcommands/g/Limit_sql.htm
Use Below query
QUERY : select * from student_table where marks in (select marks from student_table group by marks order by marks desc limit 2,1);
The following query will return all records for students in third place, regardless of how many students are tied for first, second, or third place.
SELECT student_table.name, student_table.marks
FROM student_table
WHERE student_table.primary_key IN ( SELECT ranked.primary_key
FROM student_table ranked
LEFT JOIN student_table others
ON ranked.marks < others.marks
GROUP BY 1
HAVING COUNT(DISTINCT others.marks) + 1 = 3)
For example, if student_table looks like this:
+---------+-------+
| name | marks |
+---------+-------+
| Alpha | 100 |
| Able | 100 |
| Bravo | 98 |
| Baker | 98 |
| Bone | 98 |
| Charlie | 93 | <-- 3rd place by marks
| Chimp | 93 | <-- 3rd place by marks
| Delta | 85 |
| Echo | 80 |
| Ebert | 80 |
+---------+-------+
the query will yield:
+---------+-------+
| name | marks |
+---------+-------+
| Charlie | 93 |
| Chimp | 93 |
+---------+-------+
As an aside, the query would be a little easier if MySQL supported DENSE_RANK(), but you can simulate that function with subqueries as above.