There's a table message
and a table sender
, and I want to retrieve all "message information" associated to a specific id, along with the sender name and email. I have this query:
SELECT
a.id,
a.subject,
a.body,
a.sender_id,
(
SELECT b.name
FROM sender b
WHERE b.id = a.sender_id
LIMIT 1
) AS sender_name,
(
SELECT b.email
FROM sender b
WHERE b.id = a.sender_id
LIMIT 1
) AS sender_email,
a.sent_time
FROM message a
WHERE a.id = <id>
LIMIT 1;
It works, but it has to perform two different SELECT subqueries to the same table. In this case (which is an extremely simplified example) it probably doesn't hurt the performance at all, but in a real-life scenario where a lot of fields have to be retrieved from an external table, the best way to do it is using a JOIN statement? Isn't it possible to have a SELECT subquery that retrieves many fields?
I'm using MySQL with MyISAM storage engine in case it matters.