var sidebar_align = 'right';
var content_container_margin = parseInt('290px');
var sidebar_width = parseInt('270px');
Unanswered: sub-SELECT to return two columns
Any way to join the 2 sub-selects ?
(SELECT f1 FROM `tbl2` t2 WHERE t2.`ID` = t1.`ID`) AS `f1`,
(SELECT f2 FROM `tbl2` t2 WHERE t2.`ID` = t1.`ID`) AS `f2`,
FROM `tbl1` t1
Trying to save CPU by reducing sub-SELECT queries here because both are from the same table tbl2.
(SELECT f1, f2 FROM `tbl2` t2 WHERE t2.`ID` = t1.`ID`) AS `f1`, `f2`,
Last edited by anjanesh; 09-12-07 at 05:07.
Not sure why you need a sub-select at all:
SELECT t1.ID, t2.f1, t2.f2, t1.c1, t1.c2
FROM tbl1 t1
LEFT OUTER JOIN tbl2 t2 on (t2.id = t1.id)
Also worth noting that a JOIN will be backwardly compatible with older versions of MySQL, whereas subselect is version 4.1 onwards...
Who would use such a version that doesn't even support sub-selects...
Originally Posted by aschk
someone who pre-paid $27 for seven years of hosting on 3.23 and does not want to lose the investment
Originally Posted by shammat
as russell peters would say, "YOU GO TO JAIL BAD BOY"
Im using MySQL 5.0.x and not interested in backward compatibility for this.
Thought about JOINs but for another script where I used JOIN, it took seconds to load - sometimes even minutes.
Guess I'll fallback to JOINs but is there no workaround using a single sub-query ?
Then you'll need to check your indexes.
Originally Posted by anjanesh
Usually joins are a lot faster than sub-selects (especially sub-selects in the SELECT list)
Perhaps you should perform an EXPLAIN on your SQL query(s) to find where indexes should be put.
And there is NO workaround for it. They can only ever return one row and value inside the SELECT clause.