Shalini
Shalini

Reputation: 27

to fetch records by giving id and count or limit

I have two variable id and cnt.
If id = 5 and cnt=3,
then it shows three records from id=5 from xyz table.

My query:

select * from template_master
   where  status='active' LIMIT 3 OFFSET 5

But it is returning empty result though there are records.

Upvotes: 0

Views: 146

Answers (4)

Rico_93
Rico_93

Reputation: 108

SELECT *
FROM template_master
WHERE id=5 AND cnt=3
LIMIT 3;

Upvotes: 0

JoelBonetR
JoelBonetR

Reputation: 1572

i suppose that an ID must be primary key so you will take only ONE result searching for it (or zero if theres no record with this ID), we need to know your structure to answer you correctly.

i'll explain a basic example. You wanna show 5 pictures about trees or 5 pictures about donuts if user click one or another button, ok so the database must be composed by 2 tables at least which are: images (id, image, category); categories (id, title);

Lets suppose that we want to show only trees, and lets suppose too that trees category id is 5. So your query must be:

SELECT i.image FROM images i, categories c WHERE i.category = c.id AND c.id = 5;

Am i explained well? Answer me if you have doubts, cheers! =)

Upvotes: 0

Piyush Gupta
Piyush Gupta

Reputation: 2179

Why you are using OFFSET 5?

Your query below says "return only 3 records, start on record 5.....

Try this:

select * from template_master
     where  status='active' and id >= 5  
     order by id asc LIMIT 3 OFFSET 0;

Upvotes: 2

Dhaval Bhavsar
Dhaval Bhavsar

Reputation: 495

Use these query

SELECT * FROM template_master WHERE id > 5 order by id ASC LIMIT 3

Upvotes: 0

Related Questions