get the last major and repeated records without grouping mysql

1

I have a table

id_periodo '| id_mesa
1               1
1               1
2               1
2               1

How can I select the last% co_of% greater?

I was trying

SELECT period_id, table_id FROM table where file_id = 1 ORDER BY period_id DESC limit 1 but it only shows a record

the result has to give like this

id_periodo| id_mesa

2               1
2               1
    
asked by Fmcv Mcv 17.05.2018 в 13:50
source

1 answer

0

From what I understood, what you have to do is sort the records according to your criteria, in this case id_periodo descending, then, you use LIMIT to return the number of records you need.

For example;

Table and Records:

CREATE TABLE ejemplo (
    id int NOT NULL AUTO_INCREMENT,
    id_periodo int,
    id_productos int,
    PRIMARY KEY(id)
);

INSERT INTO ejemplo (id_periodo, id_productos) 
   values
    ("1","1"),
    ("1","2"),
    ("2","2"),
    ("2","1")

Query:

SELECT *
FROM ejemplo
ORDER BY id_periodo DESC
LIMIT 2

Result:

| id | id_periodo | id_productos |
|----|------------|--------------|
|  4 |          2 |            1 |
|  3 |          2 |            2 |

I leave the SQLFiddle so you can see it in practice

EDIT

After reading your comment, to do what you need, you can do a Subquery , it would be as follows;

SELECT *
FROM ejemplo
WHERE id_periodo = (SELECT max(id_periodo) from ejemplo)

Result

| id | id_periodo | id_productos |
|----|------------|--------------|
|  3 |          2 |            2 |
|  4 |          2 |            1 |

SQLFiddle

    
answered by 17.05.2018 / 14:14
source