Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select specific row from mysql table

Ideally I need a query that is equivalent to

select * from customer where row_number() = 3 

but that's illegal.

I can't use an auto incremented field.

row_number() is the row that needs to be selected.

How do I go about this?

EDIT: Well, I use iSql*plus to practice, and using limit and auto_increment is illegal for some reason. I ended up creating a sequence and a trigger and just upped the id by 1 every time there was an entry.

like image 372
Nu Gnoj Mik Avatar asked May 04 '12 23:05

Nu Gnoj Mik


People also ask

How do I select a specific row in a table 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 specific in MySQL?

If you want to select only specific columns, replace the * with the names of the columns, separated by commas. The following statement selects just the name_id, firstname and lastname fields from the master_name table.

How do I select a single row?

In SQLJ, a single-row query can be executed and its result set data can be retrieved with a single statement: SELECT ... INTO <select target list> . The INTO-clause contains a list of host variables or host expressions that receive the result set data.


2 Answers

You can use LIMIT 2,1 instead of WHERE row_number() = 3.

As the documentation explains, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return.

Keep in mind that it's an 0-based index. So, if you want the line number n, the first argument should be n-1. The second argument will always be 1, because you just want one row. For example, if you want the line number 56 of a table customer:

SELECT * FROM customer LIMIT 55,1 
like image 158
sp00m Avatar answered Sep 26 '22 08:09

sp00m


You cannot select a row like that. You have to specify a field whose values will be 3

Here is a query that will work, if the field you are comparing against is id

select * from customer where `id` = 3 
like image 40
Starx Avatar answered Sep 26 '22 08:09

Starx