私はこの2つのクエリを持っています:
SELECT `a`.*, `b`.`id` AS host_id, SUM(CASE WHEN c.event_id IS NOT NULL THEN 1 ELSE 0 END) AS count_joins, SUM(CASE WHEN c.event_id IS NOT NULL AND c.user_id = 0 THEN 1 ELSE 0 END) AS joined
FROM (`events` AS a)
INNER JOIN `users` AS b ON `b`.`id` = `a`.`host_id`
LEFT JOIN `joins` AS c ON `c`.`event_id` = `a`.`id`
WHERE `a`.`date` > '2012-07-12 11:51:34'
GROUP BY `a`.`id`
ORDER BY `a`.`date` ASC
LIMIT 20
と
SELECT `b`.`id`, `b`.`first_name`, `b`.`last_name`, `b`.`email`, `b`.`username`, `b`.`thumbnail`
FROM (`joins` AS a)
INNER JOIN `users` AS b ON `b`.`id` = `a`.`user_id`
WHERE `a`.`event_id` = '1'
AND `a`.`user_id` != 0
ORDER BY RAND()
LIMIT 8
最初のイベントはすべてのイベントを取得し、次にforeachループを使用して、2番目のクエリで各イベントの結合を取得します。
私の質問は、1つのクエリでこれらすべてをどのように実行できるかということです。
これが私のスキーマです:
たとえば、多次元配列を返したいのですが。
Array
(
[0] => Array
(
[id] => 1
[title] => Title
[description] => DescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescription
[segment] => title
[thumbnail] => 356a192b7913b04c54574d18c28d46e6395428ab.jpeg
[cover] => 356a192b7913b04c54574d18c28d46e6395428ab.jpeg
[locale] => Locale
[address] => Rua Afonso Pena, 22, Tijuca
[list] => 0
[date] => 2013-10-10 10:10:10
[created] =>
[host_id] => 1
[count_joins] => 5
[joined] => 0
[joins] => Array
(
[0] => Array
(
[id] => 4
[first_name] => Giovanna
[last_name] => Carneiro
[email] => gigi@gmail.com
[username] => gigi
[thumbnail] => 1b6453892473a467d07372d45eb05abc2031647a.jpg
)
[1] => Array
(
[id] => 5
[first_name] => Júlio
[last_name] => César
[email] => jujuba@gmail.com
[username] => jujuba
[thumbnail] => ac3478d69a3c81fa62e60f5c3696165a4e5e6ac4.jpg
)
[2] => Array
(
[id] => 3
[first_name] => Claudio
[last_name] => Cardozo
[email] => cazo66@gmail.com
[username] => cazo
[thumbnail] => 77de68daecd823babbb58edb1c8e14d7106e83bb.jpg
)
[3] => Array
(
[id] => 1
[first_name] => Claudius
[last_name] => Ibn
[email] => ibnclaudius@gmail.com
[username] => ibnclaudius
[thumbnail] => 356a192b7913b04c54574d18c28d46e6395428ab.jpeg
)
[4] => Array
(
[id] => 2
[first_name] => Elza
[last_name] => Virginia
[email] => elza.mosqueira@gmail.com
[username] => elzavirginia
[thumbnail] => da4b9237bacccdf19c0760cab7aec4a8359010b0.jpg
)
)
)
[1] => Array
(
[id] => 2
[title] => Another Title
[description] => Description
[segment] => another-title
[thumbnail] => da4b9237bacccdf19c0760cab7aec4a8359010b0.jpeg
[cover] => da4b9237bacccdf19c0760cab7aec4a8359010b0.jpeg
[locale] => Locale
[address] => Travessa Nestor Vitor, 117, Tijuca
[list] => 0
[date] => 2013-10-10 10:10:10
[created] =>
[host_id] => 3
[count_joins] => 5
[joined] => 0
[joins] => Array
(
[0] => Array
(
[id] => 1
[first_name] => Claudius
[last_name] => Ibn
[email] => ibnclaudius@gmail.com
[username] => ibnclaudius
[thumbnail] => 356a192b7913b04c54574d18c28d46e6395428ab.jpeg
)
[1] => Array
(
[id] => 4
[first_name] => Giovanna
[last_name] => Carneiro
[email] => gigi@gmail.com
[username] => gigi
[thumbnail] => 1b6453892473a467d07372d45eb05abc2031647a.jpg
)
[2] => Array
(
[id] => 3
[first_name] => Claudio
[last_name] => Cardozo
[email] => cazo66@gmail.com
[username] => cazo
[thumbnail] => 77de68daecd823babbb58edb1c8e14d7106e83bb.jpg
)
[3] => Array
(
[id] => 5
[first_name] => Júlio
[last_name] => César
[email] => jujuba@gmail.com
[username] => jujuba
[thumbnail] => ac3478d69a3c81fa62e60f5c3696165a4e5e6ac4.jpg
)
[4] => Array
(
[id] => 2
[first_name] => Elza
[last_name] => Virginia
[email] => elza.mosqueira@gmail.com
[username] => elzavirginia
[thumbnail] => da4b9237bacccdf19c0760cab7aec4a8359010b0.jpg
)
)
)
)
最初のクエリはイベントを返し、2番目のクエリは結合を返します。可能であれば、1つのクエリだけですべてを返したいと思います。