This should be really easy but for some reason me and my brain don't want to cooperate today.
I've got a table sales that contains a brick_region column. A brick region always belongs to a region. Now I want to select all values in sales that belong to the region of the specific brick region that I've selected. How do I do this?
SELECT *
FROM sales
JOIN brick_regions
ON (brick_regions.id = sales.brick_region)
WHERE sales.date BETWEEN '2014-01-01' AND '2014-03-30'
AND sales.brick_region = 2
But I want the last line to be this instead:
AND brick_regions.region = [region ID of the brick region with ID 2]
Maybe try with a sub query?
SELECT * FROM sales
LEFT JOIN brick_regions ON brick_regions.id = sales.brick_region
WHERE sales.date BETWEEN '2014-01-01' AND '2014-03-30'
AND brick_regions.region = (SELECT region FROM brick_regions WHERE id = 2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With