SQL UNION不起作用

时间:2016-08-25 06:35:59

标签: sql sql-server

我使用的是SQL Server 2014并有两个表:ApplicationProjectLOV_SIP_ALLSTAFF

ApplicationProjectLO

|---------------------|---------------------------|
|      StaffEmailId   |             Type          |
|---------------------|---------------------------|
|       chinkim1      |              L            |
|---------------------|---------------------------|
|       kandiah1      |              A            |
|---------------------|---------------------------|

V_SIP_ALLSTAFF

|---------------------|----------------|-------------------|---------------|
|      lOGINID        |    displayname |   Email Address   |MainOfficeTelNo|
|---------------------|----------------|----------------   |-------------- |
|      chinkim1       |    james       |        1          |       5       |
|---------------------|----------------|----------------   |---------------|
|      kandiah1       |    hoho        |        2          |       8       |
|---------------------|----------------|----------------   |---------------|

我想根据' Type'。

显示信息

因此,如果StaffEmailIdLONGINID匹配且类型为A,则类型为A的信息(hoho,{{1}必须显示}和2},如果类型为8,则必须显示Ljames1

我尝试使用5UNION在一个查询中运行两个select语句,但它只根据此查询返回UNION ALL结果:

type=L

MVC查看:

SELECT
    vsa.displayname NameofLiaisonOfficer, 
    vsa.EmailAddress LOEmail, 
    '(65)' +' '+ convert(varchar, vsa.MainOfficeTelNo ) as LOContactNo,
    vsa.MainOfficeTelNo GeneralOfficeConatctNo 
FROM ApplicationProjectLo ap
INNER JOIN V_SIP_ALLSTAFF vsa ON ap.StaffEmailId = vsa.LOGINID
WHERE ap.Type = 'L'

UNION ALL

SELECT
    vsa.displayname NameofAlternateLiaisonOfficer, 
    vsa.EmailAddress AlternateLOEmail,
    '(65)' +' '+ convert(varchar, vsa.MainOfficeTelNo ) as AlternateLOContactNo,
    vsa.MainOfficeTelNo GeneralOfficeConatctNo 
FROM ApplicationProjectLo ap
INNER JOIN V_SIP_ALLSTAFF vsa ON ap.StaffEmailId = vsa.LOGINID
WHERE ap.Type = 'A'

5 个答案:

答案 0 :(得分:0)

SELECT
    vsa.displayname NameofLiaisonOfficer, 
    vsa.EmailAddress LOEmail, 
    '(65)' +' '+ convert(varchar, vsa.MainOfficeTelNo ) as LOContactNo, 
    vsa.MainOfficeTelNo GeneralOfficeConatctNo 
FROM ApplicationProjectLo ap 
INNER JOIN V_SIP_ALLSTAFF vsa ON ap.StaffEmailId = vsa.LOGINID 
WHERE ap.Type IN ('A', 'L')

答案 1 :(得分:0)

您需要JOIN,而不是UNIONJOIN根据常见key field(s)组合了两个表中的列。

SELECT a.field1, b.field1, b.field2 FROM table1 a JOIN table2 b ON a.keyfield=b.keyfield;

答案 2 :(得分:0)

只需免除WHERE

SELECT vsa.displayname as NameofLiaisonOfficer, 
       vsa.EmailAddress LOEmail, 
      '(65)' +' '+ convert(varchar, vsa.MainOfficeTelNo ) as LOContactNo,
       vsa.MainOfficeTelNo GeneralOfficeConatctNo 
FROM ApplicationProjectLo ap INNER JOIN
      V_SIP_ALLSTAFF vsa
      ON ap.StaffEmailId = vsa.LOGINID;

或使用包含两种类型的WHERE

SELECT vsa.displayname as NameofLiaisonOfficer, 
       vsa.EmailAddress LOEmail, 
      '(65)' +' '+ convert(varchar, vsa.MainOfficeTelNo ) as LOContactNo,
       vsa.MainOfficeTelNo GeneralOfficeConatctNo 
FROM ApplicationProjectLo ap INNER JOIN
      V_SIP_ALLSTAFF vsa
      ON ap.StaffEmailId = vsa.LOGINID;
WHERE ap.Type IN ('A', 'L');

那就是说,目前还不清楚为什么你的查询 - 虽然过于复杂 - 没有返回两种类型。一种可能性是你使用的是国家字符集和" A"并不是真正的" A"。

答案 3 :(得分:0)

使用以下查询显示类型为“L”和“A”的结果。

Info.plist

答案 4 :(得分:0)

const string _sqlODetails = @"SELECT vsa.displayname NameofLiaisonOfficer, 
                                                 vsa.EmailAddress LOEmail, 
                                                 '(65)' +' '+ convert(varchar, vsa.MainOfficeTelNo ) as LOContactNo, 
                                                 vsa.MainOfficeTelNo GeneralOfficeConatctNo 
                                          FROM ApplicationProjectLo ap 
                                          INNER JOIN V_SIP_ALLSTAFF vsa ON ap.StaffEmailId = vsa.LOGINID 
                                          WHERE ap.Type = 'L'";

            using (var connection = Db.SqlServer())
            {
                tpOdetails = connection
                  .Query<TPInformationDetails>(_sqlODetails)
                  .FirstOrDefault();
            }

            const string _sqlADetails = @"SELECT vsa.displayname NameofAlternateLiaisonOfficer, 
                                                  vsa.EmailAddress AlternateLOEmail,
                                                  '(65)' +' '+ convert(varchar, vsa.MainOfficeTelNo ) as AlternateLOContactNo, 
                                                  vsa.MainOfficeTelNo GeneralOfficeConatctNo 
                                          FROM ApplicationProjectLo ap 
                                          INNER JOIN V_SIP_ALLSTAFF vsa ON ap.StaffEmailId = vsa.LOGINID 
                                          WHERE ap.Type = 'A'";

            using (var connection = Db.SqlServer())

            {
                tpAdetails = connection
                  .Query<TPInformationDetails>(_sqlADetails)
                  .FirstOrDefault();
            }