Oracle SQL 中查询表值别名

发布于 2024-10-18 17:45:54 字数 654 浏览 3 评论 0原文

我有一个关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守望孤独 2024-10-25 17:45:54

技巧是:

在第一个 SELECT 中,“vendor_state”是表中字段本身的值。而且,由于 WHERE 子句,您

在第二个 SELECT 中将只有“CA”,“vendor_state”不是数据库字段中的值。相反,它只是文字值“Outside CA”的别名,

因为两个名称匹配(根据 UNION 的要求),您的最终结果“看起来”所有值都来自同一位置,但实际上并非如此。

也许这会显示得更好:

SELECT vendor_name, vendor_state, vendor_state AS vendor_state_new
  FROM vendors 
 WHERE vendor_state IN 'CA'
 UNION
SELECT vendor_name, vendor_state, 'Outside CA' AS vendor_state_new
  FROM vendors
 WHERE vendor_state NOT IN 'CA'
ORDER BY vendor_name

现在,对于这两个查询,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:

SELECT vendor_name, vendor_state, vendor_state AS vendor_state_new
  FROM vendors 
 WHERE vendor_state IN 'CA'
 UNION
SELECT vendor_name, vendor_state, 'Outside CA' AS vendor_state_new
  FROM vendors
 WHERE vendor_state NOT IN 'CA'
ORDER BY vendor_name

Now, for both queries, vendor_state show the database value. And calculated_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 my vendor_state_new: show database value for 1st, calculated value for 2nd. And the AS is ommitted in 1st SELECT because vendor_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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文