I'm new to SQL and postgreSQL and I have an assignment.
I have two tables in database:
CREATE TABLE orders(
order_id SERIAL PRIMARY KEY,
... some other fields
);
CREATE TABLE user(
customer_id SERIAL PRIMARY KEY,
customer_fname VARCHAR(30)
customer_lname VARCHAR(30)
order_id FOREIGN KEY (order_id) REFERENCES orders(order_id),
... some other fields
);
This table contains come data row with unique ID.
CREATE TABLE some_chosen_data_in_order(
customer_id SERIAL PRIMARY KEY,
id_order INTEGER[],
customer_fname VARCHAR(30),
customer_lname VARCHAR(30),
);
This table contains array type field. Each row contains values of IDs from table items in specific order. For example: {1,2,3,4}.
Now, I want to get data from table items for chosen rows from table some_chosen_data_in_order with order for elements in array type.
Using that, how can I get all of the customers and the order IDs on the users table who placed those specific orders?
My attempt was JOIN:
SELECT I.* FROM items AS I
JOIN some_chosen_data_in_order AS S ON I.customer_id = ANY(S.id_order) WHERE S.customer_id = ?