1

I have main table product, it has subtables product_store_1, ... , product_store_N based on store_id field. Product table has rules, they insert new row to one of subtables depends on store_id. It seems to work fine, but main table currently is not empty and I want to get which rule is missed. I'm trying to

SELECT store_id,count(*) FROM product GROUP BY store_id;

But it runs for all product_store_N tables, I want to run this query only for parent table.

1 Answer 1

3

You need to use ONLY in FROM clause

SELECT store_id,count(*) 
FROM ONLY product 
GROUP BY store_id;

see this example :

CREATE TABLE parent (value INT);
CREATE TABLE child_a () INHERITS (parent);
CREATE TABLE child_n() INHERITS (parent);

INSERT INTO parent VALUES (0),(1),(2),(3),(4);
INSERT INTO child_a VALUES (10),(20),(30),(40),(5);
INSERT INTO child_n VALUES (50),(60),(70),(80),(6);

if I query select * from parent where value <10 the result will be

   Value
   -----
    0
    1
    2
    3
    4
    5
    6

the last two rows that's 5 and 6 belongs to child_a and child_n respectively.

To get Value in Parent table use ONLY in FROM clause. i.e:

SELECT * FROM ONLY PARENT WHERE value <10

   Value
   -----
    0
    1
    2
    3
    4

sqlfiddle-demo

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.