How to select inconsistent rows in MySQL?

If the primary keys of records are 1,3,4,5,6,8

I want to select entries using pk: 1,6

Note

I do not know which identifiers are not sequential

+4
source share
5 answers
 SELECT * FROM your_table AS a LEFT JOIN your_table AS b ON a.key_column = b.key_column - 1 WHERE b.key_column IS NULL 
+5
source

Why not use the where clause in your SQL query?

 select * from your_table where id in (1, 6) 
+2
source

How about this?

 SELECT * FROM [MyTable] WHERE [MyId] NOT IN ( SELECT [MyId] - 1 FROM [MyTable] ) 
+2
source

Remember that in your database of choice, rows are usually not "sequential" unless you specifically order them. They simply often appear in the correct order when you do not specify the order.

+1
source

A slight improvement for the solution proposed by Robin Day

 SELECT [MyId] + 1 FROM [MyTable] WHERE [MyId] NOT IN ( SELECT [MyId] - 1 FROM [MyTable] ) ORDER BY [MyId] + 1 
0
source

Source: https://habr.com/ru/post/1304350/


All Articles