I get a lot of answers when it comes to how MySQL sorts results but none seems to address my specific issue. Hope I didn't miss the answer somewhere else here on stackoverflow.
I have an SQL query that looks like this:
SELECT id, something FROM sometable WHERE
id=1 OR id=2 OR id=5 OR
id=6 OR id=100 OR id=1000
OR id=4
Now I need it to return the results in the specific order it has been selected. The results should look like this in this specific order:
1
2
5
6
100
1000
4
Will MYSQL display the returned records in this particular order the way I selected them? I cannot use ORDER BY because I need them the exact way I selected them in the first instance. A simple test confirms that it is returned in this way but from reading elsewhere I get the idea that you cannot trust the way results are being returned in specific order when ORDER BY is not used.
You can use FIELD() function. In your application code, where you will be creating this SQL dynamically, you can build the SQL string in the same order for FIELD(), as in your WHERE clause.
FIELD(str,str1,str2,str3,...)
Returns the index (position) of str in the str1, str2, str3, ... list.
Returns 0 if str is not found.
Now, the FIELD(id, 1,2,5,6,100,1000,4) function will return 0 if the id is not in (1,2,5,6,100,1000,4).
So, if we use ORDER BY FIELD(id, 1,2,5,6,100,1000,4) only, other non-matching rows will appear at the top. So, we can use If() function to return 1 for the other non-matching rows, and 0 for the matched rows.
Now, we can utilize one more level of ordering by FIELD(id, 1,2,5,6,100,1000,4). This will ensure that the matched id(s) appear first in the order as specified in the Field() function.
SELECT id, something FROM sometable WHERE
id=1 AND id=2 AND id=5 AND
id=6 AND id=100 AND id=1000
AND id=4
ORDER BY IF(FIELD(id, 1,2,5,6,100,1000,4) = 0, 1, 0) ASC,
FIELD(id, 1,2,5,6,100,1000,4) ASC
I need to begin my selection starting with the second row of data. This query works fine
SELECT Sum(ttimediff) as 'TOTAL IDLE TIME (sec)' FROM temp where ttimediff>#3;
I just need it start with the second row, and read all rows after that.
I have tried FROM temp ORDER BY asc LIMIT 2 where ttimediff>#3; but I believe the limit statement is stopping the selection at 2. I get a syntax error
You can do this with Limit but you need to have 2 Arguments like this :
SELECT * FROM table_name LIMIT 1, 5000
This will skip 1 element and check until 5000
I want to select rows starting from a row like this:
SELECT * FROM table WHERE Type=1 LIMIT $Start,infinity
If you have a primary key say id, you can use
SELECT * FROM table WHERE Type = 1 AND id >= $Start;
There is nothing to indicate infinity in MySQL. To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter:
SELECT * FROM table WHERE Type=1 LIMIT $Start, 99999999999999999999;
I want to find the last inserted row that matches a query (ie, find the row that has the largest id among the matching rows.)
for instance, suppose the following query matches 3 rows. with ids, 1,2,and 3. I want to get only the row with id 3.
SELECT * FROM `table` WHERE `mail` = 'someone#example.com'
How do I do this?
You need to write your query like this
SELECT *
FROM table_name
WHERE `mail` = 'someone#example.com'
ORDER BY id DESC
LIMIT 1
Simply use Order By. You sort your result with Id values in decreasing order (that way, you would have maximum ID at the top, in this case 3) and then just limit your result with value 1. That would give you only one row, with max ID. So,
here goes the query:
SELECT * FROM *YourTableName* where mail = '*YourMail*'ORDER BY id DESC LIMIT 1;
Query
SELECT * FROM tbl
WHERE `mail` = 'someone#example.com'
AND id=
(
SELECT MAX(id) FROM tbl
WHERE `mail` = 'someone#example.com'
);
Fiddle demo
if I run a SELECT query that returns 10 rows, is there a way to select the 2nd item in the result set right in the SELECT statement (effectively getting a single row result)?
psedudo code:
SELECT id from MYTABLE where MYTABLE.foo = 0 and RESULT_INDEX = 2;
this would return the 2nd item from a multi item result set.
SELECT id from MYTABLE where MYTABLE.foo = 0 LIMIT 1, 1;
You'll probably want to specify an ORDER BY clause or else the nth result will be arbitrarily defined.
Edit: Oops, the first LIMIT param is zero based