Answer the question
In order to leave comments, you need to log in
SQL how to get data from the first table with the condition of having data in the second by a certain value?
Friends, I have already broken my head, tell me, please, how to build a query with the following conditions.
There are two tables, let's say article and photo.
Each article in the article table can have 0 to 5 photos of one type and 0 to 5 photos of another type. Let's say 'before' and 'after'. The column in the 'photo' table is named 'made'.
One row in the 'photo' table is one photo with either 'before' or 'after' in the corresponding field.
The request for getting articles for which there are no photos at all is like this:
SELECT title AS 'Заголовок статьи', article.id AS 'Id статьи' FROM article
LEFT JOIN `photo` ON photo.article_id = article.id
WHERE article_id IS NULL
GROUP BY article.id
Answer the question
In order to leave comments, you need to log in
It's pretty trivial:
SELECT `title` AS 'Заголовок статьи', `article`.`id` AS 'Id статьи'
FROM `article`
LEFT JOIN `photo` ON `photo`.`article_id` = `article`.`id` AND `photo`.`made` = 'before' -- or 'after'
WHERE `article_id` IS NULL
GROUP BY `article`.`id`
SELECT `title` AS 'Заголовок статьи', `article`.`id` AS 'Id статьи'
FROM `article`
WHERE NOT EXISTS (
SELECT `article_id`
FROM `photo`
WHERE `photo`.`article_id` = `article`.`id` AND `photo`.`made` = 'before' -- or 'after'
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question