结果中带有额外字段的 SQL UNION 查询

发布于 2024-11-04 04:42:48 字数 551 浏览 2 评论 0原文

我有一个运行良好的 SQL 查询,只是我想将第一个数据库中另一列的结果带入其中。这就是我目前拥有的:

SELECT parts1.PART_NBR, parts1.NIIN
FROM parts1
WHERE parts1.PART_NBR='$pn'
UNION
SELECT parts2.REFERENCE_NUMBER, parts2.NIIN
FROM parts2
WHERE parts2.REFERENCE_NUMBER='$pn'

这就是我基本上想要的(当然这不会工作):

SELECT parts1.PART_NBR, parts1.NIIN, parts1.ANOTHER_FIELD
FROM parts1
WHERE parts1.PART_NBR='$pn'
UNION
SELECT parts2.REFERENCE_NUMBER, parts2.NIIN
FROM parts2
WHERE parts2.REFERENCE_NUMBER='$pn'

如何编写查询,以便它执行相同的操作,但实际上会带回结果中的额外字段?

I have a SQL query that is working well except I would like to bring the results of another column in the first db with it. This is what I currently have:

SELECT parts1.PART_NBR, parts1.NIIN
FROM parts1
WHERE parts1.PART_NBR='$pn'
UNION
SELECT parts2.REFERENCE_NUMBER, parts2.NIIN
FROM parts2
WHERE parts2.REFERENCE_NUMBER='$pn'

This is what I basically want (of course this wont work):

SELECT parts1.PART_NBR, parts1.NIIN, parts1.ANOTHER_FIELD
FROM parts1
WHERE parts1.PART_NBR='$pn'
UNION
SELECT parts2.REFERENCE_NUMBER, parts2.NIIN
FROM parts2
WHERE parts2.REFERENCE_NUMBER='$pn'

How do I write the query so that it does the same thing but actually brings back the extra field in the results?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

始终不够 2024-11-11 04:42:48

创建一个空字段,返回 NULL

SELECT parts1.PART_NBR, parts1.NIIN, parts1.ANOTHER_FIELD
FROM parts1
WHERE parts1.PART_NBR='$pn'
UNION ALL
SELECT parts2.REFERENCE_NUMBER, parts2.NIIN, NULL AS ANOTHER_FIELD
FROM parts2
WHERE parts2.REFERENCE_NUMBER='$pn'

编辑:
从您的评论中,您看到的结果为

Part Number: 21223 NIIN: 008194914 Name: CAPACITOR
Part Number: 21223 NIIN: 011241926 Name: HEAT SINK
Part Number: 21223 NIIN: 003901600 Name: KNIFE
Part Number: 21223 NIIN: 003901600 Name: 
Part Number: 21223 NIIN: 008194914 Name:
Part Number: 21223 NIIN: 011241926 Name:

第一个结果来自表 parts1,第二个结果来自 parts2,空白的 Name 字段位于其中您正在返回NULL

从您提供的信息中,我不明白您为什么使用 UNION 来获取这两个表的结果,因为它们似乎包含相同的信息,除了第一个表也有 名称字段。

JOIN 零件/参考编号上的表格以便选择名称不是更好吗?

编辑:
正如您在评论中所说,之前由于使用 UNION,您获得了 DISTINCT 结果集。使用 NULL 字段,行不再是唯一的,查询将返回所有行。

我在评论中说,我看不到当前的 UNION 语句为您做了什么,因为两个表似乎都有相同的信息。这个询问是否比您告诉我们的更多?

Create an empty field, returning NULL

SELECT parts1.PART_NBR, parts1.NIIN, parts1.ANOTHER_FIELD
FROM parts1
WHERE parts1.PART_NBR='$pn'
UNION ALL
SELECT parts2.REFERENCE_NUMBER, parts2.NIIN, NULL AS ANOTHER_FIELD
FROM parts2
WHERE parts2.REFERENCE_NUMBER='$pn'

EDIT:
From your comment you are seeing the results as

Part Number: 21223 NIIN: 008194914 Name: CAPACITOR
Part Number: 21223 NIIN: 011241926 Name: HEAT SINK
Part Number: 21223 NIIN: 003901600 Name: KNIFE
Part Number: 21223 NIIN: 003901600 Name: 
Part Number: 21223 NIIN: 008194914 Name:
Part Number: 21223 NIIN: 011241926 Name:

The first results are coming from the table parts1 the second from parts2, the blank Name fields are where you are returning NULL.

From the information you have given I don't see why you are using a UNION to get the results from these two tables as they seem to contain the same information except the first table also has the Name field.

Would it not be better to JOIN the tables on the Part/Reference number in order to select the name?

EDIT:
As you said in your comment, previously you were getting a DISTINCT result set because of using UNION. With the NULL field the rows are no longer unique and the query returns all the rows.

I said in a comment that I do not see what the current UNION statement is doing for you as it seems the same information is both tables. Is there more to this query than what you have told us?

云胡 2024-11-11 04:42:48
SELECT parts.PART_NBR, parts.NIIN, parts1.ANOTHER_FIELD
FROM  (
        SELECT parts1.PART_NBR, parts1.NIIN
        FROM parts1
        WHERE parts1.PART_NBR = '$pn'
        UNION
        SELECT parts2.REFERENCE_NUMBER, parts2.NIIN
        FROM parts2
        WHERE parts2.REFERENCE_NUMBER =  '$pn'
      ) AS parts(PART_NBR, NIIN)
  LEFT OUTER JOIN parts1
    ON parts.PART_NBR = parts1.PART_NBR AND
       parts.NIIN = parts1.NIIN      
SELECT parts.PART_NBR, parts.NIIN, parts1.ANOTHER_FIELD
FROM  (
        SELECT parts1.PART_NBR, parts1.NIIN
        FROM parts1
        WHERE parts1.PART_NBR = '$pn'
        UNION
        SELECT parts2.REFERENCE_NUMBER, parts2.NIIN
        FROM parts2
        WHERE parts2.REFERENCE_NUMBER =  '$pn'
      ) AS parts(PART_NBR, NIIN)
  LEFT OUTER JOIN parts1
    ON parts.PART_NBR = parts1.PART_NBR AND
       parts.NIIN = parts1.NIIN      
野心澎湃 2024-11-11 04:42:48

您必须有匹配的列,但是您可以在返回 null 或任何您想要的默认值的第二个列中放置一个“假”列。

SELECT parts1.PART_NBR, parts1.NIIN, parts1.ANOTHER_FIELD
FROM parts1
WHERE parts1.PART_NBR='$pn'
UNION
SELECT parts2.REFERENCE_NUMBER, parts2.NIIN, NULL AS ANOTHER_FIELD
FROM parts2
WHERE parts2.REFERENCE_NUMBER='$pn'

You have to have matching columns, however you can place a "fake" column in the second one that comes back null or whatever default value you want.

SELECT parts1.PART_NBR, parts1.NIIN, parts1.ANOTHER_FIELD
FROM parts1
WHERE parts1.PART_NBR='$pn'
UNION
SELECT parts2.REFERENCE_NUMBER, parts2.NIIN, NULL AS ANOTHER_FIELD
FROM parts2
WHERE parts2.REFERENCE_NUMBER='$pn'
淡笑忘祈一世凡恋 2024-11-11 04:42:48

简单如下:

select part_number, niin, max(field)
from (     
    SELECT parts1.PART_NBR part_number, parts1.NIIN niin, parts1.ANOTHER_FIELD field
    FROM parts1
    WHERE parts1.PART_NBR='$pn'
    UNION
    SELECT parts2.REFERENCE_NUMBER part_number, parts2.NIIN niin, "" field
    FROM parts2
    WHERE parts2.REFERENCE_NUMBER='$pn')
group by part_number, niin

您只需在第二个查询中容纳一个占位符以匹配第一个查询的额外字段位置。

Simple as below:

select part_number, niin, max(field)
from (     
    SELECT parts1.PART_NBR part_number, parts1.NIIN niin, parts1.ANOTHER_FIELD field
    FROM parts1
    WHERE parts1.PART_NBR='$pn'
    UNION
    SELECT parts2.REFERENCE_NUMBER part_number, parts2.NIIN niin, "" field
    FROM parts2
    WHERE parts2.REFERENCE_NUMBER='$pn')
group by part_number, niin

You just need to accommodate a placeholder in the second query to match the extra field location of the first.

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