MySQL - How to query for one value but default to another? - mysql

I asked the question about multiple rows in a previous thread, but now I need to know about a single row. This is an example query:
SELECT * FROM table1 WHERE this = 5 LIMIT 1
But if there is no rows found then I would it to then find the record with a different value:
SELECT * FROM table1 WHERE this = 1 LIMIT 1
So what I want is, find a row with this having the value of 5, if not found then default to finding a row with a value of 1. How do I do that in a single query? Would an OR statement work like so?
SELECT * FROM table1 WHERE this = 5 OR this = 1 LIMIT 1
Would the order of the OR statement try to find any row with the value of 5, if found then it will stop and return the row? IF not found then look for 1 and then return that row? If not then how do I do this with a single query where it searches for one value, if not found defaults to find another value?
I was thinking this query:
SELECT * FROM table1 WHERE this IN(5,1) ORDER BY this ASC LIMIT 1
However this query would always return the value with 1 if both values were found right? So I tried this:
SELECT * FROM table1 WHERE this IN(5,1) ORDER BY this DESC LIMIT 1
But this would always return the value of 5 if both rows were found correct?

Your question appears to prioritize the search for 5 and only if not found then search for 1. Hence your second solution fits the requirement fine. It will return the value of 5 irrespective of if 5 is only found or both 5 and 1 exist.
the 2nd option
SELECT * FROM table1 WHERE this IN(5,1) ORDER BY this DESC LIMIT 1

Try this
SELECT *
FROM table1 tab
WHERE tab.this = IF((SELECT COUNT(*)
FROM table1
WHERE this = 5) > 0, 5, 1)
The query inside the IF is checking for the count.
I am not sure about the performance but I think this solves your issue.

Related

mysql default sort order with specific selects

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

begin selection with second row of data

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

How to select rows staring from a row whithout limits?

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;

Find the last inserted row that matches a query

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

is it possible to select a specific row index from a mysql select result set?

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