PostgreSQL单查询从两个表中选择SELECT单行而没有JOIN [英] PostgreSQL single query SELECT single row FROM two tables without JOIN

查看:93
本文介绍了PostgreSQL单查询从两个表中选择SELECT单行而没有JOIN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建的查询结果将仅返回一行。我需要提取的数据来自两个没有任何关系列的表。第二个表每年仅添加一次附加行,因此我将根据需要通过PHP构造查询。

The result of a query I need to create will only return a single row. The data I need to pull comes from two tables without any relational columns. The second table only has additional rows added once per year so I'll construct the query via PHP as necessary.

我知道如何使用SQL子选择,但是我m 确保在没有关系时如何 SELECT 多列 FROM 第二个表

I know how to use SQL sub-SELECTs however I'm not sure how to SELECT multiple columns FROM a second table when there is no relational data in a performance oriented/dynamic way.

这是一个静态示例,其中我使用多个子SELECT来可视化我要执行的操作...

Here is a static example where I use multiple sub-SELECTs to visualize what I'm trying to do...

SELECT t1.a, 
t1.b, 
t1.c, 
(SELECT t2.d FROM table2) AS d, 
(SELECT t2.e FROM table2) AS e, 
(SELECT t2.f FROM table2) AS f, 
(SELECT t2.g FROM table2) AS g, 
t1.h, 
t1.i
FROM table1 AS t1;

如何动态,有效地从第二个没有关系列的表中提取多个列表?

How do I dynamically and efficiently pull multiple columns from a second table that has no relational columns with the first table?

我不想创建第二个单独的查询,因为这将是便宜的解决方案,很可能会对性能产生某些影响,而最坏的影响我不会扩展我对SQL的理解。

I do not want to create a second separate query as it would be cheap solution, most likely have some impact on performance and worst of all I wouldn't expand my understanding of SQL.

推荐答案

听起来像您需要笛卡尔联接(无联接)-但您会将这些值相乘(即,如果表1有100行,表2有10行,您将返回1000行)

Sounds like you need a cartesian join (no join) -- but you WILL multiply the values together (ie, if table 1 has 100 rows and table 2 has 10 rows, you'll return 1000 rows)

SELECT t1.a, 
t1.b, 
t1.c, 
t2.d, 
t2.e, 
t2.f,
t2.g,
t1.h, 
t1.i
FROM table1 AS t1, table2 as t2;

这篇关于PostgreSQL单查询从两个表中选择SELECT单行而没有JOIN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆