0

I want to fetch 3 rows data from class table who is belongs to class 5,6 and 7 last row only.

Right now I'm using 3 sql queries to fetch data:

 1. SELECT * from class where class_name = '5';

 2. SELECT * from class where class_name = '6';

 3. SELECT * from class where class_name = '7';

How can I use single sql query to retrieve 3 row data instead of 3 sql queries ?

**class table structure:**

|id | class_name | student  |
|---| -----------| ---------| 
|1  |      5     | Student A|
|2  |      6     | Student B|
|3  |      4     | Student C|
|4  |      6     | Student D|
|5  |      7     | Student E|
|6  |      5     | Student F|
|7  |      4     | Student G|
|8  |      6     | Student H|
|9  |      5     | Student I|
|10 |      6     | Student J|
|11 |      7     | Student K|
|12 |      6     | Student L|
|13 |      8     | Student M|
|14 |      6     | Student N|
|15 |      8     | Student O|

Result required: Student I,Student N and Student K

Sagar Gangwal
  • 7,544
  • 3
  • 24
  • 38
John
  • 51
  • 1
  • 3
  • 13
  • i have update my answer....with sqlfiddle – Bilal Ahmed Jun 07 '17 at 06:38
  • This is the most frequently asked question under this tag. Occasionally, it's correctly answered too. For a definitive solution, please refer to the manual https://dev.mysql.com/doc/refman/5.7/en/example-maximum-column-group-row.html – Strawberry Jun 07 '17 at 06:42

6 Answers6

1

try this

SELECT * FROM class WHERE id IN (SELECT MAX(id) FROM class WHERE class_name in (5,6,7) GROUP BY class_name );

sqlfiddle

Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42
0

select * from (select * from class order by id desc, class_name) x group by class_name

MZaragoza
  • 10,108
  • 9
  • 71
  • 116
Tushar Walzade
  • 3,737
  • 4
  • 33
  • 56
0
SELECT * from (SELECT * from class ORDER BY id  DESC) t WHERE class_name in (5,6,7) GROUP BY class_name ORDER BY id DESC 
king
  • 300
  • 1
  • 13
0

Use a subquery since GROUP BY cannot be used with a wildcard. Instead you have to group by every column that you select.

SELECT * FROM class
WHERE id IN (
    SELECT MAX(id) FROM class
    WHERE class_name IN (5, 6, 7) GROUP BY class_name ORDER BY id DESC
);
Praind
  • 1,551
  • 1
  • 12
  • 25
0
SELECT * FROM class 
WHERE id IN (
    SELECT MAX(ID) 
    FROM dbo.class 
    WHERE class_name IN (5,6,7) 
    GROUP BY class_name )

This should do the trick, tested and here's the result:

id          class_name student
----------- ---------- --------------------
1           5          A                   
2           6          B                   
3           4          C                   
4           6          D                   
5           7          E                   
6           5          F                   
7           4          G                   
8           6          H                   
9           5          I                   
10          6          J                   
11          7          K                   
12          6          L                   
13          8          M                   
14          6          N                   
15          8          O                   

(15 row(s) affected)

id          class_name student
----------- ---------- --------------------
9           5          I                   
11          7          K                   
14          6          N                   

(3 row(s) affected)
Paul Karam
  • 4,052
  • 8
  • 30
  • 53
-1

SELECT * from class where class_name = '5' or class_name = '6' or class_name = '7'

wijaya
  • 152
  • 7