简体   繁体   English

我使用连接和联合的 SQL 查询有什么问题?

[英]What is wrong with my SQL query which uses Joins and Unions?

What's wrong with my sql query?我的 sql 查询有什么问题? I am trying to use a Join and at the same time a UNION to get all table from another table while joining other tables together based on a relationship ..我正在尝试使用JoinUNION从另一个表中获取所有表,同时根据关系将其他表连接在一起..

However I get the following error:但是我收到以下错误:

"The used SELECT statements have a different number of columns"

My query:我的查询:

SELECT a.ESN, a.UnixTime, a.Payload, a.Timestamp
                ,b.AlarmingStatus
                ,b.STxModel
                ,c.GroupID
                FROM STxMessage a
                JOIN STx b ON b.ESN = a.ESN
                JOIN GroupInfo c ON b.GroupID = c.GroupID
                WHERE b.STxModel = 190
                AND a.AlarmsChecked="y"
                AND c.AlertsMasterSwitch="on"
               UNION ALL 
      SELECT d.ESN , d.UnixTime,  d.Payload,  d.Timestamp FROM STxMessageArchive d

The error message says it all.错误消息说明了一切。

When using UNION , the columns return by the combined SELECT statement must be the same, eg.使用UNION ,组合SELECT语句返回的列必须相同,例如。

SELECT col1, col2, col3 FROM table1
UNION
SELECT col1, col2, col3 FROM table2

if the columns do not match, you can still combine it provided that you have to provide dummy data for the column, eg如果列不匹配,您仍然可以组合它,前提是您必须为列提供虚拟数据,例如

SELECT col1, col2, col3 FROM table1
UNION
SELECT col1, col2, '' AS col3 FROM table2

so in your query, it should look like this所以在你的查询中,它应该是这样的

SELECT  a.ESN, a.UnixTime, a.Payload, a.Timestamp ,
        b.AlarmingStatus, b.STxModel, c.GroupID
FROM    STxMessage a
        INNER JOIN STx b 
            ON b.ESN = a.ESN
        INNER JOIN GroupInfo c 
            ON b.GroupID = c.GroupID
WHERE   b.STxModel = 190 AND 
        a.AlarmsChecked="y" AND 
        c.AlertsMasterSwitch="on"
UNION ALL 
SELECT  d.ESN, d.UnixTime, d.Payload, d.Timestamp,
        NULL AS AlarmingStatus, NULL AS STxModel, NULL AS GroupID
FROM    STxMessageArchive d

hi this are the extra columns in your first query嗨,这是您的第一个查询中的额外列

 ,b.AlarmingStatus
 ,b.STxModel
 ,c.GroupID

you need this same columns in second query to do union or you need to remove this column to do union operation您需要在第二个查询中使用相同的列进行union或者您需要删除此列以进行union操作

As the error says, the first part has 7 columns, and the second part has only 4. An unions needs to have the same columns on both sides.正如错误所说,第一部分有 7 列,第二部分只有 4 列。联合需要在两侧具有相同的列。 Either remove要么删除

b.AlarmingStatus ,b.STxModel ,c.GroupID 

from the first part, or add (even bogus) columns in the second part.从第一部分,或在第二部分添加(甚至是假的)列。

Your first query is selecting 7 columns where as the second query is only selecting 4. You need to make sure the second query is selecting the same number of columns as the first to make the Union All work.您的第一个查询选择了 7 列,而第二个查询仅选择了 4 列。您需要确保第二个查询选择的列数与第一个相同,以使Union All工作。

SELECT a.ESN, a.UnixTime, a.Payload, a.Timestamp
                ,b.AlarmingStatus
                ,b.STxModel
                ,c.GroupID
                FROM STxMessage a
                JOIN STx b ON b.ESN = a.ESN
                JOIN GroupInfo c ON b.GroupID = c.GroupID
                WHERE b.STxModel = 190
                AND a.AlarmsChecked="y"
                AND c.AlertsMasterSwitch="on"
               UNION ALL 
      SELECT d.ESN , d.UnixTime,  d.Payload,  d.Timestamp, 'null' as AlarmingStatus,
      'null' as STxModel, 'null' as GroupID FROM STxMessageArchive d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM