MySQL Query Bug? - mysql

I do that query that should return me 5 query but I get 10...
SELECT *
FROM article ar, account ac
WHERE ar.approved = '1'
AND ar.author = ac.id
ORDER BY ar.id DESC
LIMIT 5 , 10
Showing rows 0 - 9 (10 total, Query took 0.0028 sec)
What am I doing wrong? It was working fine before...

In mySQL LIMIT X, Y means
X is starting element (offset)
Y is number of elements that you want to be returned
that's why you're getting 10 rows back.
If you only want 5 rows back and you need 5 first rows to be skipped, you should use LIMIT 5, 5.

As you need only 5 rows and you need to skip the first 5 rows use:
LIMIT(5,5)

Try this:
SELECT *
FROM article ar, account ac
WHERE ar.approved = '1'
AND ar.author = ac.id
LIMIT 5, 10
ORDER BY ar.id DESC
Here is the grammer:
The offset(5) specifies the offset of the first row to return. The
offset of the first row is 0, not 1.
The count(10) specifies maximum number of rows to return.

Related

Select a specific number of records from a record

I have this Query that is working normally but now I need to show only 4 record from record 5 of the database?
How would this query look?
Select noticias.id,
idn,
titulo,
chamada,
foto_a,
subcatid,
noticias_subcategorias.subcategoria,
slug,
noticias.rlogdata
From noticias,
noticias_subcategorias
Where noticias.subcatid = noticias_subcategorias.id
And noticias.status = 1
And posicao = 1
And noticias.tipo = 2
Order By idn Desc
Limit 0, 4
Thanks.
If you want to start at record 5, you'd use LIMIT 4 OFFSET 4 to skip the first 4 records.

Mysql Limit Offset not working

Here is my query, which i am using for pagination
SELECT DISTINCT email_list.*, email_counter.phone as e_phone,email_counter.email as e_email,email_counter.marketing as e_marketing
FROM Data_TLS_builders as email_list
LEFT JOIN wp_pato_email_list_counters as email_counter on email_counter.email_id = email_list.URN
LIMIT 120 OFFSET 150
Rather than starting at 120 and finishing at 150, which should display 30 results, mysql is returning 120 results and ignoring the OFFSET. I have tried LIMIT 120,150 and still the same?
Any idea how to solve?
SELECT DISTINCT email_list.*, email_counter.phone as e_phone,email_counter.email as e_email,email_counter.marketing as e_marketing FROM Data_TLS_builders as email_list LEFT JOIN wp_pato_email_list_counters as email_counter on email_counter.email_id = email_list.URN LIMIT 30 OFFSET 120
Limit specifies number of records.
OFFSET specifies upto how many records it should skip.
The above query returns 30 records from 121.
You have to switch the limit parameters.
Solution 1:
SELECT DISTINCT email_list.*, email_counter.phone as e_phone,email_counter.email as e_email,email_counter.marketing as e_marketing FROM Data_TLS_builders as email_list LEFT JOIN wp_pato_email_list_counters as email_counter on email_counter.email_id = email_list.URN LIMIT 30 OFFSET 120
Solution 2:
SELECT DISTINCT email_list.*, email_counter.phone as e_phone,email_counter.email as e_email,email_counter.marketing as e_marketing FROM Data_TLS_builders as email_list LEFT JOIN wp_pato_email_list_counters as email_counter on email_counter.email_id = email_list.URN LIMIT 120, 30
You can also have a look into the documentation: http://www.w3schools.com/php/php_mysql_select_limit.asp.
LIMIT will tell mysql HOW MANY results you want to show.
OFFSET will tell mysql where to START.
if you want to display 30 results starting at 120, it should be:
LIMIT 30 OFFSET 120
or
LIMIT 120, 30

selecting the first value that equals a number or letter sql php

I have a db with 6 columns: Taarij, Mispar ,Tiltan, Ale, Yaalom and Lev.
I have a query that brings me the 50 last results ordered by Mispar, that works.
select * from Hagrala order by Mispar desc limit 0,50
I want from that query result to find some data.
For example I want to find the value of the field Mispar where Ale = 7 the first time.
What I mean that 7 can and will appear several times in the column called Ale, but I am looking in which Mispar is the highest.
If in a row Mispar = 30765 and Ale = 7 and in another row Mispar = 30755 and Ale=7 also I want to receive only 30765.
Just use order by and limit:
select *
from Hagrala
where Ale = 7
order by Mispar desc
limit 1;

"LIMIT 5,10" returns 9 rows in MySQL?

http://666kb.com/i/c6e4rv80gz9yrn9h5.png
Check out my image, I limited the query for just 5 entry but it returns with 9 rows? What is the problem, I could not get it.
With mysql, the LIMIT parameters are offset, row_count, but the first parameter is optional - crazy, but true!
So when you have two parameters, the first is the starting row, the second is the number of rows.
You asked for LIMIT 5, 10 which means 10 rows, starting from row 5 (not rows 5 to 10).
You are not the first, and you won't be the last, person to be confused by this.
In MySQL Limit function Syntax are:
SELECT column_name(s)
FROM table_name
LIMIT Rows start , Quantity of Rows for display;
Example:
SELECT *
FROM customer
LIMIT 2 , 10;
I suggest go to this link LIMIT in mysql, after I browsed through the above comments, I have to point out, that limit 5, 10 returns 10 rows and it starts from row 6. LIMIT: limit 10 offset 5 and limit 5, 10, both of them return 10 rows, starting from row 6. I strongly recommend go to the above link.
Hope it helps!!

how can I tell if the last x rows of 'state' = 1

I need help with a SQL query.
I have a table with a 'state' column. 0 means closed and 1 means opened.
Different users want to be notified after there have been x consecutive 1 events.
With an SQL query, how can I tell if the last x rows of 'state' = 1?
If, for example, you want to check if the last 5 consecutive rows have a state equals to 1, then here's you could probably do it :
SELECT IF(SUM(x.state) = 5, 1, 0) AS is_consecutive
FROM (
SELECT state
FROM table
WHERE Processor = 3
ORDER BY Status_datetime DESC
LIMIT 5
) as x
If is_consecutive = 1, then, yes, there is 5 last consecutive rows with state = 1.
Edit : As suggested in the comments, you'll have to use ORDER BY in your query, to get the last nth rows.
And for more accuracy, since you have a timestamp column, you should use Status_datetime to order the rows.
You should be able to use something like this (replace the number in the HAVING with the value of x you want to check for):
SELECT Processor, OpenCount FROM
(
SELECT TOP 10 Processor, DateTime, Sum(Status) AS OpenCount
FROM YourTable
WHERE Processor = 3
ORDER BY DateTime DESC
) HAVING OpenCount >= 10