Show latest results postgresql

0

I need to display the last 5 results of a query, but I do this query twice because I need to change the where clause to return the values I need.

I thought of something like:

SELECT id, project_id, name, subject FROM "issues" where project_id = 94 

UNION ALL 
    SELECT id, project_id, name, subject FROM "issues" where project_id = 95 

limit 5

But it did not work ... Is there any way to do a query only and change its clause to display all the records I need?

    
asked by anonymous 20.03.2017 / 15:26

2 answers

1

To solve your problem you can use the IN clause. It allows you to enter a set of values for a test. In this case, your query would be:

SELECT id, project_id, name, subject FROM "issues" where project_id IN (94, 95) limit 5
The IN tests whether the project_id value is between any of the reported values (94 or 95) and is listed in the results.

    
20.03.2017 / 15:55
0

You can create a SQL command that meets your needs:

SELECT id, project_id, name, subject from "issues" where project_id = 94 and project_id = 95 order by id desc limit 5

Where where project_id = 94 and project_id = 95 already checks both conditions. Order by id desc takes the last information first. Limit 5 Get your last 5 in case.

    
20.03.2017 / 16:01