SELECT `id1`, `id2`, `id3`, COUNT(*) AS `count`, `p2`
FROM `table`
GROUP BY `id1`, `id2`, `id3`, `p2`
ORDER BY NULL
-- Запрос на выборку всех квартир, свободных на определенную дату
SELECT * FROM Apartment a
WHERE NOT EXISTS (
SELECT 1 FROM Apartment_Dates
WHERE apartment_id = a.id AND ? BETWEEN arrival_time AND departure_time
);
-- Запрос на выборку всех квартир, свободных в определенный промежуток дат
SELECT * FROM Apartment a
WHERE NOT EXISTS (
SELECT 1 FROM Apartment_Dates
WHERE apartment_id = a.id AND
(arrival_time BETWEEN ? AND ? OR departure_time BETWEEN ? AND ?)
);
-- Запрос на выборку всех квартир, занятых в определенный промежуток дат
SELECT * FROM Apartment a
WHERE EXISTS (
SELECT 1 FROM Apartment_Dates
WHERE apartment_id = a.id AND
(arrival_time BETWEEN ? AND ? OR departure_time BETWEEN ? AND ?)
);
SELECT id, SUBSTRING_INDEX(SUBSTRING_INDEX(groups, ',', g.n), ',', -1) groupName
FROM (
SELECT @n:=@n+1 n FROM
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) s0,
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) s1,
(SELECT @n:=0) sg
) g
JOIN Item ON CHAR_LENGTH(groups)-CHAR_LENGTH(REPLACE(groups, ',', '')) >= g.n-1
ORDER BY id, n
SELECT u.*, t2.date FROM users u LEFT JOIN (
SELECT @rn := IF(t1.prevdate IS NULL, 1, @rn + 1) rn, t1.id, t1.user_id, t1.date FROM (
SELECT o.id, o.user_id, o.date, o2.date prevdate
FROM orders o
LEFT JOIN orders o2 ON o2.user_id = o.user_id AND o2.date < o.date
LEFT JOIN orders o3 ON o3.user_id = o.user_id AND o3.date > o2.date AND o3.date < o.date
WHERE o3.id IS NULL ORDER BY o.user_id, o.date
) t1, (SELECT @rn := 0) sg
) t2 ON t2.user_id = u.id AND t2.rn = 2
SELECT CONV(hash, 16, 10) ^ 0xd9d8266fd990160e FROM table
SELECT DISTINCT IF(sender_id > recipient_id, sender_id, recipient_id) id1, IF(sender_id < recipient_id, sender_id, recipient_id) id2 FROM dialog
UPDATE product SET disabled = 1 WHERE id IN (
SELECT product.id FROM product LEFT JOIN product_image ON product.id = product_image.product_id GROUP BY product.id HAVING COUNT(product_image.id) = 0);
UPDATE product SET disabled = 1 WHERE NOT EXISTS (SELECT 1 FROM product_image WHERE product_id = product.id)