Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select non-consecutive rows in MySQL?

Tags:

select

mysql

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

I want to select the records with pk:1,6

NOTE

I don't know which ids are non-consecutive

like image 247
mysql Avatar asked Mar 17 '10 11:03

mysql


People also ask

How do I select only few rows in mysql?

The following is the syntax to get the last 10 records from the table. Here, we have used LIMIT clause. SELECT * FROM ( SELECT * FROM yourTableName ORDER BY id DESC LIMIT 10 )Var1 ORDER BY id ASC; Let us now implement the above query.

How do I only show a specific row in SQL?

To select rows using selection symbols for character or graphic data, use the LIKE keyword in a WHERE clause, and the underscore and percent sign as selection symbols. You can create multiple row conditions, and use the AND, OR, or IN keywords to connect the conditions.

How do I select all rows except one?

Select the header or the first row of your list and press Shift + Ctrl + ↓(the drop down button), then the list has been selected except the first row. Note: If there are blank cells in your list, it will select the list until the first blank cell appears with this method.

How do I select only a row?

Or click on any cell in the column and then press Ctrl + Space. Select the row number to select the entire row. Or click on any cell in the row and then press Shift + Space. To select non-adjacent rows or columns, hold Ctrl and select the row or column numbers.


2 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
like image 71
LukeH Avatar answered Oct 10 '22 17:10

LukeH


Why not use a where clause in your SQL query ?

select *
from your_table
where id in (1, 6)
like image 36
Pascal MARTIN Avatar answered Oct 10 '22 17:10

Pascal MARTIN