SELECT `sa_real`.*
FROM (
SELECT DISTINCT `location_id_fk`
FROM `sa_metro_house`
WHERE `metro_id_fk` IN (4,7,6)
) AS `t`
JOIN `sa_real` ON `sa_real`.`house_location_id_fk` = `t`.`location_id_fk`
SELECT *
FROM `sa_real`
WHERE `house_location_id_fk` IN (
SELECT `location_id_fk`
FROM `sa_metro_house`
WHERE `metro_id_fk` IN (4,7,6)
)
SELECT `t`.`id_project`, `p`.`title`, `t`.`task_count`,
`t`.`task_lose`, `t`.`task_active`
FROM (
SELECT `id_project`, COUNT(*) AS `task_count`,
SUM(`status` = '1' AND `deadlines` < :TIME) AS `task_lose`,
SUM(`status` = '1') AS `task_active`
FROM `tasks`
GROUP BY `id_project`
) AS `t`
RIGHT JOIN `projects` AS `p` ON `p`.`id` = `t`.`id_project`
ORDER BY `t`.`task_lose` DESC, `t`.`task_active` DESC,
`t`.`task_count` DESC
SELECT '2017-01-01' + INTERVAL `d`.`dayOfYear` DAY + INTERVAL `h`.`hour` HOUR
FROM (
SELECT (`c`.`num`*100+`d`.`num`*10+`s`.`num`) AS `dayOfYear`
FROM (
SELECT 0 AS `num`
UNION SELECT 1
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5
UNION SELECT 6
UNION SELECT 7
UNION SELECT 8
UNION SELECT 9
) AS `s`
JOIN (
SELECT 0 AS `num`
UNION SELECT 1
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5
UNION SELECT 6
UNION SELECT 7
UNION SELECT 8
UNION SELECT 9
) AS `d` ON TRUE
JOIN (
SELECT 0 AS `num`
UNION SELECT 1
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5
UNION SELECT 6
UNION SELECT 7
UNION SELECT 8
UNION SELECT 9
) AS `c` ON TRUE
HAVING `dayOfYear` < DAYOFYEAR('2017-12-31')
) AS `d`
JOIN (
SELECT 0 AS `hour`
UNION SELECT 1
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5
UNION SELECT 6
UNION SELECT 7
UNION SELECT 8
UNION SELECT 9
UNION SELECT 10
UNION SELECT 11
UNION SELECT 12
UNION SELECT 13
UNION SELECT 14
UNION SELECT 15
UNION SELECT 16
UNION SELECT 17
UNION SELECT 18
UNION SELECT 19
UNION SELECT 20
UNION SELECT 21
UNION SELECT 22
UNION SELECT 23
) AS `h` ON TRUE
SELECT `t`.*
FROM (
SELECT MAX(`UpdateDate`) AS `last`, `OrgPropertTypeId`
FROM `table` AS `t`
WHERE `OrganizationId` = :organizationId
GROUP BY `OrgPropertTypeId`
) AS `l`
JOIN `table` AS `t` ON `t`.`OrganizationId` = :organizationId
AND `t`.`OrgPropertTypeId` = `l`.`OrgPropertTypeId`
AND `t`.`UpdateDate` = `l`.`last`
SELECT `a`.`id` AS `id`, `a`.`name` AS `name`,
`color`.`value` AS `color`, `weight`.`value` AS `weight`
FROM `a`
LEFT JOIN `b` AS `color` ON `b`.`a_id` = `a`.`id` AND `b`.`name` = 'color'
LEFT JOIN `b` AS `weight` ON `b`.`a_id` = `a`.`id` AND `b`.`name` = 'weight'
SELECT `h`.*, `h1`.`author`
FROM `helpdesk` AS `h`
LEFT JOIN (
SELECT `t1`.`parent` AS `parent`, `t2`.`author` AS `author`
FROM (
SELECT `parent`, MAX(`datetime`) AS `maxtime`
FROM `helpdesk`
WHERE `parent` != 0
GROUP BY `parent`
) AS `t1`
JOIN `helpdesk` AS `t2` ON `t2`.`parent` = `t1`.`parent`
AND `t2`.`datetime` = `t1`.`maxtime`
) AS `h1` ON `h1`.`parent` = `h`.`id`
WHERE `h`.`parent` = 0
Необходимо разбивать строку на массив и работать с каждым элементом массива в отдельности?Если у вас в поле MySQL хранится список чего-либо через запятую, и по этому списку нужен поиск, то вы изначально неправильно продумали схему базы данных.