views:

66

answers:

3

I saw somewhere what seemed to be nested selects, one "master" select on the "outside" and a series of selects inside- is this possible? I'm not talking about joins as there is particular relation between the selects.

I seem not to be explaining myself very well. I want to do a single query which will pull out a series of stats from various tables latest order, latest customer, largest order. Obviously I can do that with a series of selects.

The example I saw was something like

select (

 select ... from tbl_1 where ..,
 select ... from tbl_2 where ..,
 select ... from tbl_3 where ..,
 ...
)
+2  A: 

Possibly UNION?

select *
from (
    select name, value from Table1
    union all
    select name, value from Table2
) a
order by name
RedFilter
+2  A: 

I think you're asking about "Subselects" (aka Derived Tables, Nested Queries).

Eg//

SELECT colA, colB
FROM tableA
    INNER JOIN (
                SELECT colB, fk
                FROM tableB
                WHERE colC = 'Blah'
               ) ON tableA.pk = tableB.fk
ORDER BY colA, colB

If so, they've been available since v4.1 of MySQL. [1]

There's a bit of a performance problem with them, so be wary if you think they'll work as well as they do in other RDBMS. [2]

[1] http://dev.mysql.com/doc/refman/5.0/en/subqueries.html

[2] http://www.mysqlperformanceblog.com/2006/08/31/derived-tables-and-views-performance/

James
A: 

In fact all I needed was following (sorry)

SELECT d.parameter_value,tr.PARAMETER_VALUE 
FROM `maindb`.`tbl_parameter_despatch` AS d,tbl_parameter_transactionid AS tr ; 
zzapper