Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does MySQL "SELECT LIMIT 1" with multiple records select first record from the top?

I've searched and searched and can't find an answer to this question, I'm probably asking it in the wrong way.

I am querying an employee database.

I need to get details based on a position id, however there could be multiple records for that position id as this organisation has permanent employees and temporary employees that act against the same position.

So, in order to get the CURRENT occupant of the position id, I need my query to SELECT the FIRST record that matches the position string, from the TOP DOWN.

will this select the first matched record from the top?

SELECT * WHERE `position_id`="00000000" LIMIT 1;

Thanks in advance.

like image 389
hammus Avatar asked Jul 07 '13 22:07

hammus


People also ask

What does LIMIT 1 do in MySQL?

In MySQL the LIMIT clause is used with the SELECT statement to restrict the number of rows in the result set. The Limit Clause accepts one or two arguments which are offset and count. The value of both the parameters can be zero or positive integers.

How do I LIMIT a selection in MySQL?

Assume we wish to select all records from 1 - 30 (inclusive) from a table called "Orders". The SQL query would then look like this: $sql = "SELECT * FROM Orders LIMIT 30"; When the SQL query above is run, it will return the first 30 records.

How do I select multiple records in MySQL?

To select multiple values, you can use where clause with OR and IN operator.

Is there any LIMIT on in clause in MySQL?

From my experience the maximum values is 1000 values in clause IN ('1',....,'1000') , I have 1300 value in my excel sheet,I put them all into IN ,MySQL return only 1000 .


2 Answers

You need an ORDER BY clause to define the ordering between the individual records your table. If you do not use ORDER BY you can assume no fixed order between the records, and you could get a new order each time you executed the query.

like image 143
apartridge Avatar answered Oct 06 '22 08:10

apartridge


From the manual:

With one argument, the value specifies the number of rows to return from the beginning of the result set

So with LIMIT 1 you get the first row from the result set. What the result set is depends on engine used and which indexes you have. If you want the first row added, you need to create another column to define that.

like image 36
flogvit Avatar answered Oct 06 '22 08:10

flogvit