0

I want to use

select * from table limit x;

This x can change dynamically. I don't want to use number. There are a solution for limit that use parameter?

4
  • 1
    Where does this limit come from? Is it calculated within postgresql? Commented Nov 29, 2016 at 12:00
  • It comes from database as a column Commented Nov 29, 2016 at 12:01
  • Can you give an exmple? Why do you store limits inside a column? How are they calculated? Commented Nov 29, 2016 at 12:02
  • I want to call table according to months. If its 31,30,29,28 etc. Program calculate months and I want to use it as limit. there is a way in postgresql for this? Commented Nov 29, 2016 at 12:06

2 Answers 2

2

Sure you can. Never needed to myself but:

SELECT * FROM some_table LIMIT (SELECT a_limit FROM other_table);

Obviously that sub-query should only return one row.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes it does. I ran it before posting.
0

To further expand on Richard Huxton's answer, this could be used in a LATERAL sub-query, for example to dynamically limit a K-Nearest Neighbours query:

SELECT loc_id, num_neighbours, neighbour_id
FROM locations,
LATERAL (SELECT neighbour_id 
         FROM neighbours 
         ORDER BY ST_distance(locations.geom, neighbours.geom) 
         LIMIT num_neighbours) knn

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.