Oracle SQL 中查询表值别名
我有一个关于 Oracle 10g 的 SQL 的家庭作业,其中我必须将 union 应用于两个不同的 select 语句,以返回两列。我需要vendor_state下每个单元格的值来指示CA,并需要另一个状态中的每个其他值来返回“外部CA”,以指示它们在其他地方。
我应用了联合并生成了两列和 CA 列表,但列出了许多其他状态 ID,并且我找不到如何更改查询本身中的实际值的解释。最终,我偶然发现了一个答案,但我无法解释为什么会这样。代码如下:
SELECT vendor_name,
vendor_state
FROM vendors
WHERE vendor_state IN 'CA'
UNION
SELECT vendor_name,
'Outside CA' AS vendor_state
FROM vendors
WHERE vendor_state NOT IN 'CA'
ORDER BY vendor_name
这给了我所需的确切答案,但我不知道为什么第二个 select 语句中的别名会这样......我的教科书中没有给出任何解释,我也没有读过任何内容表示列别名可以这样完成。但是,通过切换列名和别名值,我替换了返回的值而不是列名本身......我不是在抱怨结果,但如果我知道我是如何做到的,那将会有所帮助。
I have a homework assignment in SQL for Oracle 10g where I have to apply union to two different select statements, to return two columns. I need the values of each cell under vendor_state to indicate CA and every other value in another state to return "Outside CA", to indicate they're elsewhere.
I applied the union and produced the two columns and the listings for CA, but many other state IDs were listed and I couldn't find an explanation for how to change the actual values in the query itself. Eventually, I stumbled on an answer, but I can't explain why this works. The code is as follows:
SELECT vendor_name,
vendor_state
FROM vendors
WHERE vendor_state IN 'CA'
UNION
SELECT vendor_name,
'Outside CA' AS vendor_state
FROM vendors
WHERE vendor_state NOT IN 'CA'
ORDER BY vendor_name
This gives me the exact answer I need, but I don't know why the aliasing in the second select statement can behave this way....no explanation is given in my textbook and nothing I've read indicates that column aliasing can be done like this. But, by switching the column name and the alias value, I have replaced the value being returned rather than the column name itself...I'm not complaining about the result, but it would help if I knew how I did it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
技巧是:
在第一个 SELECT 中,“vendor_state”是表中字段本身的值。而且,由于 WHERE 子句,您
在第二个 SELECT 中将只有“CA”,“vendor_state”不是数据库字段中的值。相反,它只是文字值“Outside CA”的别名,
因为两个名称匹配(根据 UNION 的要求),您的最终结果“看起来”所有值都来自同一位置,但实际上并非如此。
也许这会显示得更好:
现在,对于这两个查询,
vendor_state
显示数据库值。calculated_state
显示第一个 SELECT 的数据库值,以及第二个 SELECT 的“外部 CA”。在您的查询中,
vendor_state
扮演我的vendor_state_new
的角色:显示第一个的数据库值,第二个的计算值。并且AS
在第一个 SELECT 中被省略,因为vendor_state ASvendor_state
是多余的(但可以出于澄清目的而这样做,并且当您混合时这样做是一个很好的习惯)给定列名称中的计算值和“原始”值)The trick is:
In the 1st SELECT, "vendor_state" is the value of the field itself, from the table. And, because of the WHERE clause, you will only have 'CA'
In the 2nd SELECT, "vendor_state" is NOT the value from the database field. Rather, its merely an alias for the literal value 'Outside CA'
Because both names match (as required by the UNION), your end result "looks like" all values came from same place, when in fact they didnt.
Maybe this will show better:
Now, for both queries,
vendor_state
show the database value. Andcalculated_state
show the database value for the 1st SELECT, and the 'Outside CA' for the 2nd.In your query,
vendor_state
do the role of myvendor_state_new
: show database value for 1st, calculated value for 2nd. And theAS
is ommitted in 1st SELECT becausevendor_state AS vendor_state
would be redundant (but it can be done for clarification purposes, and its a nice habit to do so when you mix calculated and "raw" values in a given colum name)