SELECT product_id FROM
(
SELECT product_id FROM T WHERE av_id = 3
UNION ALL
SELECT product_id FROM T WHERE av_id = 11
UNION ALL
SELECT product_id FROM T WHERE av_id = 15
) AS F
GROUP BY product_id
HAVING COUNT(*) = 3
SELECT product_id FROM
(
SELECT product_id FROM T WHERE av_id = 3
UNION ALL
SELECT product_id FROM T WHERE av_id = 2
) AS F
GROUP BY product_id
HAVING COUNT(*) = 2
CREATE TABLE Point ( P CHAR(2), X INT, Y INT);
INSERT Point VALUES ( 'A1', 3, 4 ), ( 'A2', 8, 7 );
SELECT CONCAT( PA1.P, PA2.P ), PA2.X - PA1.X, PA2.Y - PA1.Y
FROM Point AS PA1, Point AS PA2
WHERE PA1.P = 'A1' AND PA2.P = 'A2'
Как это организовать в одном SQL запросе?Никак без указания СУБД.
CREATE TABLE Test ( start_time TIME, end_time TIME );
INSERT INTO Test VALUES ('00:30:00', '02:00:00'), ('23:00:00', '02:00:00'), ('23:00:00', '00:30:00');
SELECT *, TIME_TO_SEC('01:10:10') AS W,
TIME_TO_SEC(CASE WHEN start_time > end_time THEN ( start_time - INTERVAL 24 HOUR ) ELSE start_time END) AS B,
TIME_TO_SEC(end_time) AS E
FROM Test
W
попадает в интервал 1 и 2.W
и нужно ли TIME_TO_SEC('23:10:10') - 86400
, что бы он попал в 2 и 3 интервал? GROUP BY events.id
CREATE TABLE `author` (
`id` INT(10) NOT NULL,
`name` VARCHAR(200) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
`fam` VARCHAR(200) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
`birthday` DATE NULL DEFAULT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `books` (
`id` INT(10),
`id_author` INT(10) NULL DEFAULT NULL,
`book` VARCHAR(500) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
PRIMARY KEY (`id`),
INDEX `id_author` (`id_author`)
);
INSERT INTO `author` (`id`, `name`, `fam`, `birthday`) VALUES (1, 'Николай Николаевич', 'Носов', '2008-11-23');
INSERT INTO `author` (`id`, `name`, `fam`, `birthday`) VALUES (2, 'Артур Конан', 'Дойль', '1859-05-22');
INSERT INTO `books` (`id`, `id_author`, `book`) VALUES (1, 1, 'Незнайка учится');
INSERT INTO `books` (`id`, `id_author`, `book`) VALUES (2, 1, 'Незнайка-путешественник');
INSERT INTO `books` (`id`, `id_author`, `book`) VALUES (3, 1, 'Винтик, Шпунтик и пылесос');
SELECT a.id, a.name, a.fam, GROUP_CONCAT(DISTINCT b.book ORDER BY b.book ASC SEPARATOR ', ') AS books
FROM `author` a
LEFT JOIN `books` b ON a.id=b.id_author
GROUP BY a.id;