开发者

Joining a subselect to a table in sql

Is it possible to join the results of a SELECT with another table.

Like this: SELECT * FROM table1 LEFT OUTER JOIN (SELECT * FROM table 2开发者_开发百科)

I know I need to link the column but am not sure how. Is this possible?


You need to know what columns you are joining on. Assuming they are called ID in both tables, something like this would work:

SELECT *
FROM table1 t1
LEFT OUTER JOIN (SELECT * FROM table 2) t2 on t1.ID = t2.ID

Note that rather than using *, you should name the columns you need explicitly. This will give a more efficient query if you do not need all of the data, and will also prevent duplicate column names from being returned.


You can do this. The code would be something like:

(SELECT id as leftid, [other fields] FROM table1) LEFT OUTER JOIN (SELECT id rightid, [other fields] FROM table2) ON (leftid=rightid)

I didn't test it, though...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜