0

Link that i referred to -> MySQL - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery

I have a teacher that has a code and a mac address attached to the router.. the students have to enter the same code from the same mac address.. i want a list of students who have done it ... and i want to only check the last code uploaded by the teacher

thank you for all the help :)

My code(which is working)->

SELECT s.s_id
FROM students s
WHERE s.mac IN
    (SELECT *
     FROM
       (SELECT t.mac
        FROM teacher t
        ORDER BY t.sno DESC
        LIMIT 1)temp_tab)
  AND s.code IN
    (SELECT *
     FROM
       (SELECT t.code
        FROM teacher t
        ORDER BY t.sno DESC
        LIMIT 1)temp_tab);
Community
  • 1
  • 1
Mihirp
  • 55
  • 4
  • 1
    What exactly are you trying to do? An example would be very helpful. – Giorgos Altanis Apr 15 '17 at 21:38
  • I have a teacher that uploads a code and a mac address attached to the router.. the students have to enter the same code from the same mac address.. i want a list of students who have done it ... and i want to only check the last code uploaded by the teacher – Mihirp Apr 15 '17 at 22:11

4 Answers4

3

Using a join should be more efficient, but then again, it depends on what you are trying to achieve.

SELECT s.s_id
FROM students s
JOIN (
    SELECT t.mac, t.code
    FROM teacher t
    ORDER BY t.sno DESC
    LIMIT 1
) t ON s.mac = t.mac AND s.code = t.code

Indexing the matching or ordering columns would increase the performance too.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
Viliam Aboši
  • 447
  • 2
  • 14
  • I have a teacher that uploades a code and a mac address attached to the router.. the students have to enter the same code from the same mac address.. i want a list of students who have done it ... and i want to only check the last code uploaded by the teacher – Mihirp Apr 15 '17 at 22:11
0

You can use limit 1 with =:

SELECT s.s_id
FROM students s
WHERE s.mac = (SELECT t.mac
               FROM teacher t
               ORDER BY t.sno DESC
               LIMIT 1
              ) AND
     s.code = (SELECT t.code
               FROM teacher t
               ORDER BY t.sno DESC
               LIMIT 1
              );

However, this code is probably not what you want. It is fine if there are no ties for t.sno. But if they are the two fields may not come from the same row. So, do it in one subquery:

SELECT s.s_id
FROM students s
WHERE (s.mac, s.code) = (SELECT t.mac, t.code
                         FROM teacher t
                         ORDER BY t.sno DESC
                         LIMIT 1
                        );

You can, of course, turn this into a JOIN as Viliam suggests.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

instead of select s.s_id it should be * Thanks Gordon and Villiam

SELECT * FROM students s JOIN ( SELECT t.mac, t.code FROM teacher t ORDER BY t.sno DESC LIMIT 1 ) t ON s.mac = t.mac AND s.code = t.code

Mihirp
  • 55
  • 4
0

Why does everyone think they have to run every SQL task in a single query?

Here's a much easier solution:

SELECT t.mac, t.code
FROM teacher t
ORDER BY t.sno DESC
LIMIT 1
INTO @mac, @code;

SELECT s.s_id
FROM students s
WHERE s.mac = @mac AND s.code = @code;
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828