SELECT service.id AS service_id, COUNT(subs.id) AS c
FROM ( SELECT 13 id UNION
SELECT 74 UNION
SELECT 71 UNION
SELECT 72 ) AS service
LEFT JOIN Subscriptions subs ON subs.service_id = service.id AND subs.msisdn=992777757031
GROUP BY service.id
ORDER BY c DESC;
Как бы вы поступили в подобной ситуации.
WITH
cte1 AS ( SELECT id, name, ROW_NUMBER() OVER (ORDER BY name) rn
FROM t1 ),
cte2 AS ( SELECT id, name, ROW_NUMBER() OVER (ORDER BY name) rn
FROM t2 )
SELECT cte1.id id1, cte1.name name1, cte2.id id2, cte2.name name2
FROM cte1
JOIN cte2 USING (rn, id);
SELECT u.*, t.type_name, t.description
FROM users AS u
LEFT JOIN type_user AS tu ON tu.id_user = u.id
LEFT JOIN types AS t ON tu.id_type = t.id
WHERE u.id = 1;
WITH RECURSIVE
cte1 AS ( SELECT MIN(DATE(create_date)) mindate, MAX(DATE(create_date)) maxdate
FROM shops
UNION ALL
SELECT MIN(DATE(create_date)) mindate, MAX(DATE(create_date)) maxdate
FROM users ),
cte2 AS ( SELECT MIN(mindate) mindate, MAX(maxdate) maxdate
FROM cte1 ),
dates AS ( SELECT mindate thedate, maxdate
FROM cte2
UNION ALL
SELECT thedate + INTERVAL 1 DAY, maxdate
FROM dates
WHERE thedate < maxdate ),
shopstat AS ( SELECT DATE(create_date) thedate, COUNT(*) cnt
FROM shops
GROUP BY thedate ),
userstat AS ( SELECT DATE(create_date) thedate, COUNT(*) cnt
FROM users
GROUP BY thedate )
SELECT thedate `date`,
COALESCE(shopstat.cnt, 0) shops,
COALESCE(userstat.cnt, 0) users
FROM dates
LEFT JOIN shopstat USING (thedate)
LEFT JOIN userstat USING (thedate)
SELECT event,
CONVERT_TZ(time, '+00:00', '-04:00') AS time1
FROM events
WHERE CONVERT_TZ(time, '+00:00', '-04:00') between '2021-06-24 00:00:00' AND '2021-06-24 23:59:59'
AND lang = 'ru'
SELECT event,
CONVERT_TZ(time, '+00:00', '-04:00') AS time1
FROM events
WHERE lang = 'ru'
HAVING time1 between '2021-06-24 00:00:00' AND '2021-06-24 23:59:59'
UPDATE users u1
CROSS JOIN users u2
SET u1.amount = u1.amount - $summa,
u2.amount = u2.amount + $summa
WHERE u1.id = $client
AND u2.id = $shop;
SELECT t1.*
FROM table t1
WHERE EXISTS ( SELECT NULL
FROM table t2
WHERE t1.id <> t2.id -- выражение первичного ключа
AND t1.column = t2.column -- для всех полей, кроме первичного ключа
DELETE
FROM table t1
WHERE EXISTS ( SELECT NULL
FROM table t2
WHERE t1.id > t2.id -- оставить только запись с минимальным ID
AND t1.column = t2.column
SELECT value,
SUM(value = val1) total_1,
SUM(value = val2) total_2
FROM ( SELECT val1 value FROM test
UNION
SELECT val2 FROM test ) total
CROSS JOIN test
GROUP BY value
ORDER BY value;