SQL查询SELECT FROM [从表名选择值] [英] SQL query SELECT FROM [Select value from tablename]

查看:254
本文介绍了SQL查询SELECT FROM [从表名选择值]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从名称在另一个表中的表中获取数据.

I am trying to get data from a table whose name is in another table.

选择* from(从tab1中选择tab1.value加入tab1.id = tab2.id上的tab2)

select * from (select tab1.value from tab1 join tab2 on tab1.id = tab2.id )

当我尝试此操作时,我得到的是tab1.value而不是外部选择*

When i try this i get tab1.value instead of the outer select *

有没有办法从外部选择中获取数据?

Is there a way to get data from the outer select ?

让我将其分解为两个sql语句,使操作变得简单

let me break this into two sql statements to make is little easier

1)从tab1中选择tab1.value加入tab1.id = tab2.id上的tab2

1) select tab1.value from tab1 join tab2 on tab1.id = tab2.id

使用上面的tab1.value作为2的表名

use the tab1.value from above as table name for 2)

2)从tab1.value中选择*

2) select * from tab1.value

推荐答案

本质上,选择*"从嵌入式视图返回结果集.如果要显示来自tab1的更多信息,则需要使用该信息.事实是您根本不需要这样做:

Essentially, "select *" is returning the result set from your inline view. If you want more information from tab1 to display, you need to use that. The truth is you shouldn't really need to do this at all:

select tab1.value, tab1.other_column, tab1.other_column_i_want, etc 
  from tab1
  join tab2 on tab2.id = tab1.id;

不需要内联视图.顺便说一句,"SELECT *"是不好的做法.

There is no need for the inline view. By the way, "SELECT *" is bad practice.

这样想:如果我问您钥匙扣中的一把钥匙,然后期望您钥匙圈中的所有钥匙作为答案的一部分,您会认为我(充其量)有一颗螺丝松动.

Think of it this way: If I ask you for one key from your keyring, then expect all the keys from your keyring as part of the answer, you would think I had (at best) a screw loose.

您最初是在要求tab1.value.您无法从该结果集中提取其他列.

You are initially asking for tab1.value. You cannot extract other columns from that result set.

这篇关于SQL查询SELECT FROM [从表名选择值]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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