SELECT *
FROM members m
JOIN (SELECT *
FROM members
WHERE logins_by_ip > 1
GROUP BY ip_address
HAVING COUNT(1) > 1) AS sel ON m.member_id = sel.member_id
ORDER BY m.ip_address
SUBSTRING_INDEX(ip_address, '.', 2)
SUBSTRING_INDEX(ip_address, '.', 3)
SELECT Item.*
FROM Item i
JOIN (
SELECT
o.item_id
, MIN(o.price) AS minPrice
FROM Offer o
GROUP BY o.item_id
) sel ON sel.item_id = i.id
WHERE i.minPrice != sel.minPrice
SELECT names.Name
FROM humans
JOIN names ON humans.Number = name.Number
WHERE humans.HumanID = 1;
SELECT f1, @i := @i + 1
FROM table1
JOIN (SELECT @i := 0) t
SELECT f1, f2
FROM
(
SELECT f1, f2, @i := @i + 1 AS num
FROM table1
JOIN (SELECT @i := 0) t
ORDER BY f3
) sel
WHERE sel.num % 5 = 0
select -(`id`) from `dialogs` where # Получаем список диалогов
`deleted`='0' and # Не удалённых
`owner`='1' and # Основная запись
`id` in
(
select `id` from `dialogs` where # Диалоги
`user`=:u and # В которых я включен
`deleted`='0' # И из которых меня не удалили
)
select -(`id`) from `dialogs` where # Получаем список диалогов
`deleted`='0' and # Не удалённых
`owner`='1' and # Основная запись
`user`=:u # В которых я включен
SELECT COUNT(1)
FROM
(
SELECT 1
FROM messages m1
WHERE m.to = :u
AND m.deleted_to = '0'
AND m1.unread = '1'
AND m1.from != :u
UNION ALL
SELECT 1
FROM messages m2
JOIN dialogs d1 ON m.to = -(d1.id)
WHERE d1.deleted = '0'
AND d1.owner = '1'
AND d1.user = :u
AND m2.unread = '1'
AND m2.from != :u
) sel