Use of Limit in mysql - mysql

First query is giving fine results but question is about 2nd
When I have given limit 2,2. How two rows can be selected? this is the weied beahaviour query
SELECT Ordinal_Position, Column_Name FROM information_schema.columns WHERE
table_schema = 'accounts' AND table_name = 'chequeout' LIMIT 2 , 2
What is wrong with 2nd query or its sum bug? What could be the solution?
Expected result of second query is a single row with values 2 and Amount

Limit 2, 2 means start at record 2 (the third 0,1,2) and display 2 rows
What you seem to want to do is :
WHERE Ordinal_Position = 2

You're probably a little confused about how to use LIMIT:
LIMIT <offset>,<limit>
<offset> means starting row index and <limit> tells how much rows.

Related

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 do I select the top 3 starting at row 2?

I never played with the MySQL syntax so I don't know how to do this right now.
I know in SQL it would be
select top 3 cloumn_name
from table_name
MySQL
select column_name
from table_name
limit 3, 2;
can someone explain how limit works?
Use limit and offset:
select t.*
from table_name t
order by ??
limit 3 offset 1;
When using limit you should generally use order by. In addition, for offset purposes, MySQL starts counting at 0 and not 1.

get records from table starting from row 5 onwards

I have a mysql Database and I want to get the records starting from row 5 onwards. This is what i am sing right now
SELECT * FROM categories WHERE status = 1
limit 5 18446744073709551615 ORDER BY sortOrder ASC
But it is throwing me an error
Error Executing Database Query.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '18446744073709551615
ORDER BY
sortOrder ' at line 6
Also i am not sure at this point if this is the correct way of doing it or not
Thanks
You require OFFSET
Example:
SELECT something FROM table LIMIT $limit OFFSET $offset;
//or alternatively
SELECT something FROM table LIMIT $offset,$limit;
Regarding your error:
Remove the number - what is it for in the first place?
Your statement should be:
SELECT * FROM categories WHERE status = 1
OFFSET 5 ORDER BY sortOrder ASC
You missed a comma , in your sql query.. just change it to the following:
SELECT * FROM categories WHERE status = 1
limit 5, 18446744073709551615 ORDER BY sortOrder ASC
↑
Explanation: LIMIT with two arguments (comma separated) will take first argument as OFFSET and second as the maximum number of rows to return.
Alternatively, if you just want to get all rows from a particular record.. you can use OFFSET keyword. The following will fetch rows starting from 5th row.
SELECT * FROM categories WHERE status = 1
OFFSET 5 ORDER BY sortOrder ASC
Perhaps You need to just Rearrange the sequence of Instruction.
Make Sure 'sortOrder' column exists in your Categories table.
The Following Query should work,
Select * from Categories Where status =1
ORDER BY sortOrder ASC
Limit 4, 999980090909
If you want to get the rows starting from 5 , then your Limit value should start from 4 and the second value can be some larger number.
Hope this helps you!

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

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.

Numbers in SELECT statements

I encountered sql queries like 'SELECT 1 FROM TABLE_NAME' or 'SELECT 2 FROM TABLE_NAME' while debugging a program. I am curious as to what the numbers in the queries do and what result set does it return.
Thanks
It will return a single column with the number as value, with n rows, n being the number of rows returned from the select.
This is usefull in cases such as
SELECT *
FROM YourTable yt
WHERE EXISTS(
SELECT 1
FROM SomeOtherTable sot
WHERE yt.ID = sot.ID
)
Also, good article at SELECT 1 vs SELECT * – An Interesting Observation
Select 1 from your_table --> On execution , you get the value 1 for every row in your_table.
'Select *' and 'Select 1 or 2' have the same performance when executing without an EXIST condition. My personal choice is using 'Select 1 or 2' when there are conditions to check for existing rows as it is slightly faster, for eg. when querying with VIEWS or temp tables having millions of rows and lot many columns.