I
I
Ivan Churkin2015-06-10 02:55:59
PostgreSQL
Ivan Churkin, 2015-06-10 02:55:59

How to implement the following SQL query?

The first table is "places".

id serial not null PKey,
nm text,
param1 integer not null,
param2 integer not null,
param3 integer not null,
...
param4 integer not null

The second table is "equi".
id serial not null PKey,
name text

Third table "example" (places <->> equi)
place_id integer not null FKey (place),
equi_id integer not null FKey (equi)

In the third table, both columns form the primary key.
It is necessary to select such records from the "places" table, which will correspond to n-parameters from the same table:
SELECT id FROM places WHERE param1 = 5 ... paramN = 3

AND THAT n-records from the "example" table.
Matching means that for a specific id from "places" both WHERE places.param1 = 5 AND ... AND places.paramN = 3 and WHERE example.equi_id = 7 are performed, only example.equi_id can be n-th number . And I don't understand how to put it in a request.
I hope I explained clearly. Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2015-06-10
@ivanchurkin

SELECT id 
    FROM places p
    JOIN (
        SELECT place_id 
            FROM example 
            WHERE equi_id IN (1, 2, 3)
            GROUP BY place_id
            HAVING COUNT(place_id) = 3
        ) e ON e.place_id = p.id
    WHERE p.param1 = 1 AND p.param2 = 2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question