I have 3 tables:
The product
table has a field group_id
(is the product's group).
The movement_product
table has a field movement_id
(matches with movement
table) and product_id
(matches with the product
table).
I want to select all the movement products of a movement if one of them is from a specific group of products.
I tried the following SQL:
SELECT
mp.*
FROM
movement AS m,
movement_product AS mp
WHERE
m.id = mp.movement_id
AND EXISTS (
SELECT *
FROM product p
WHERE mp.product_id = p.id
AND p.group_id = "48"
)
GROUP BY mp.movement_id
But it is returning a list just with the products that are from that specific group (the 48 on group_id is just an example).
If I understand correctly, you can follow this logic. First, to get all the movements that qualify:
select distinct mp.movement_id
from movement_products mp
where exists (select 1
from products p
where p.id = mp.product_id and
p.group_id = 48
);
To get the products in these movements, you can do:
select mp.*
from movement_products mp
where mp.movement_id in (select mp.movement_id
from movement_products mp
where exists (select 1
from products p
where p.id = mp.product_id and
p.group_id = 48
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.