Обсуждение: Select clause in JOIN statement

Поиск
Список
Период
Сортировка

Select clause in JOIN statement

От
JORGE MALDONADO
Дата:
Is it valid to specify a SELECT statement as part of a JOIN clause?

For example:

SELECT table1.f1, table1.f2 FROM table1
INNER JOIN 
(SELECT table2.f1, table2.f2 FROM table2) table_aux ON table1.f1 = table_aux.f1

Respectfully,
Jorge Maldonado

Re: Select clause in JOIN statement

От
Luca Vernini
Дата:
It works.
Also consider views.

Just used this on a my db:

SELECT * FROM tblcus_customer
INNER JOIN
( SELECT * FROM tblcus_customer_status WHERE status_id > 0) AS b
ON tblcus_customer.status = b.status_id


You can even join with a function result.

Regards,

Luca.

2013/6/14 JORGE MALDONADO <jorgemal1960@gmail.com>:
> Is it valid to specify a SELECT statement as part of a JOIN clause?
>
> For example:
>
> SELECT table1.f1, table1.f2 FROM table1
> INNER JOIN
> (SELECT table2.f1, table2.f2 FROM table2) table_aux ON table1.f1 =
> table_aux.f1
>
> Respectfully,
> Jorge Maldonado



Re: Select clause in JOIN statement

От
Andreas Joseph Krogh
Дата:
På fredag 14. juni 2013 kl. 01:10:51, skrev Luca Vernini <lucazeo@gmail.com>:
It works.
Also consider views.

Just used this on a my db:

SELECT * FROM tblcus_customer
INNER JOIN
( SELECT * FROM tblcus_customer_status WHERE status_id > 0) AS b
ON tblcus_customer.status = b.status_id
 
This query is the same as a normal JOIN:
SELECT *
FROM tblcus_customer
    INNER JOIN
    tblcus_customer_status b
        ON tblcus_customer.status = b.status_id AND b.status_id > 0
or
SELECT *
FROM tblcus_customer
    INNER JOIN
    tblcus_customer_status b
        ON tblcus_customer.status = b.status_id
WHERE b.status_id > 0
But you can JOIN on SELECTs selecting arbitrary stuff.
 
--
Andreas Joseph Krogh <andreak@officenet.no>      mob: +47 909 56 963
Senior Software Developer / CTO - OfficeNet AS - http://www.officenet.no
Public key: http://home.officenet.no/~andreak/public_key.asc