PostgreSQL single query SELECT single row FROM two tables without JOIN

user3750809

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.

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.

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?

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.

Joe Love

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;

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Using WITH + DELETE clause in a single query in postgresql

来自分类Dev

SELECT query from 2 tables

来自分类Dev

SQL query inner join tables, print to HTML <select> tag

来自分类Dev

MySQL SELECT from two tables with COUNT

来自分类Dev

Fetch single value with linq projection query without using FirstOrDefualt

来自分类Dev

Select same fields from multiple tables using postgresql

来自分类Dev

子查询结果 'Single Row Sub-Query results more than One Row'

来自分类Dev

Select single column as list from existing list using fieldname as string

来自分类Dev

get single value from sql-query into a textbox

来自分类Dev

Deleting csv file vales from mysql table using single query

来自分类Dev

How to get a single value from gae ndb query?

来自分类Dev

Combining info from two API feeds into single array

来自分类Dev

PHP and MySQL Select a Single Value

来自分类Dev

Spring + Hibernate: How do I efficiently chain two link tables and include resulting data in single entity? (user-role-right)

来自分类Dev

MySQL Query 2 Tables Join列旁边

来自分类Dev

MySQL Query 2 Tables Join列旁边

来自分类Dev

Codeigniter, join of two tables with a WHERE clause

来自分类Dev

rating bar not show all star in single row

来自分类Dev

Postgresql: How to relate data between two tables?

来自分类Dev

Kendo UI Grid: Select single cell, get back DataItem, and prevent specific cells from being selected?

来自分类Dev

I am trying to select data from 2 mysql tables into 1 query

来自分类Dev

Select count from four tables

来自分类Dev

Can we have single trigger for multiple tables in MySQL

来自分类Dev

Select query with left outer join and sum with group by

来自分类Dev

PostgreSQL - column value changed - select query optimization

来自分类Dev

在 Mysql 中使用 Query 将多列JOIN TABLES

来自分类Dev

jquery parse xml from single and multiple tags

来自分类Dev

how to take a single record from an arraylist in mongodb

来自分类Dev

Yii2 Hide Actions in Action Column of a Single Row only

Related 相关文章

  1. 1

    Using WITH + DELETE clause in a single query in postgresql

  2. 2

    SELECT query from 2 tables

  3. 3

    SQL query inner join tables, print to HTML <select> tag

  4. 4

    MySQL SELECT from two tables with COUNT

  5. 5

    Fetch single value with linq projection query without using FirstOrDefualt

  6. 6

    Select same fields from multiple tables using postgresql

  7. 7

    子查询结果 'Single Row Sub-Query results more than One Row'

  8. 8

    Select single column as list from existing list using fieldname as string

  9. 9

    get single value from sql-query into a textbox

  10. 10

    Deleting csv file vales from mysql table using single query

  11. 11

    How to get a single value from gae ndb query?

  12. 12

    Combining info from two API feeds into single array

  13. 13

    PHP and MySQL Select a Single Value

  14. 14

    Spring + Hibernate: How do I efficiently chain two link tables and include resulting data in single entity? (user-role-right)

  15. 15

    MySQL Query 2 Tables Join列旁边

  16. 16

    MySQL Query 2 Tables Join列旁边

  17. 17

    Codeigniter, join of two tables with a WHERE clause

  18. 18

    rating bar not show all star in single row

  19. 19

    Postgresql: How to relate data between two tables?

  20. 20

    Kendo UI Grid: Select single cell, get back DataItem, and prevent specific cells from being selected?

  21. 21

    I am trying to select data from 2 mysql tables into 1 query

  22. 22

    Select count from four tables

  23. 23

    Can we have single trigger for multiple tables in MySQL

  24. 24

    Select query with left outer join and sum with group by

  25. 25

    PostgreSQL - column value changed - select query optimization

  26. 26

    在 Mysql 中使用 Query 将多列JOIN TABLES

  27. 27

    jquery parse xml from single and multiple tags

  28. 28

    how to take a single record from an arraylist in mongodb

  29. 29

    Yii2 Hide Actions in Action Column of a Single Row only

热门标签

归档