SQL查询 - 返回找到数据的字段

时间:2017-03-29 23:30:23

标签: sql-server

我在SQL中有一个电话号码搜索查询,我想知道如何在显示的字段数据中显示列。有没有办法做到这一点? 这是我的问题:

Select DISTINCT ST.SCHOOLID, 
ST.STUDENT_NUMBER,
ST.LASTFIRST, 
ST.GRADE_LEVEL
FROM PS.STUDENTS ST
LEFT OUTER JOIN PS.U_GUARDIANS GA
ON ST.DCID = GA.STUDENTSDCID
LEFT OUTER JOIN PS.U_DEMO DE
ON ST.DCID = DE.STUDENTSDCID
LEFT OUTER JOIN PS.U_EMERGENCY EME
ON ST.DCID = EME.STUDENTSDCID
LEFT OUTER JOIN PS.U_STUDENT_HISTORY PREV
ON ST.DCID = EME.STUDENTSDCID
WHERE ST.Home_Phone LIKE '%param1%'
OR DE.STUDENT_CELL LIKE '%param1%'
OR GA.G1_PHONE1 LIKE '%param1%'
OR GA.G1_PHONE2 LIKE '%param1%'
OR GA.G1_WORK_PHONE LIKE '%param1%'
OR GA.G2_PHONE1 LIKE '%param1%'
OR GA.G2_PHONE2 LIKE '%param1%'
OR GA.G2_WORK_PHONE LIKE '%param1%'
OR GA.G3_PHONE1 LIKE '%param1%'
OR GA.G3_PHONE2 LIKE '%param1%'
OR GA.G3_WORK_PHONE LIKE '%param1%'
OR GA.G4_PHONE1 LIKE '%param1%'
OR GA.G4_PHONE2 LIKE '%param1%'
OR GA.G4_WORK_PHONE LIKE '%param1%'
OR EME.EC1_Phone1 LIKE '%param1%'
OR EME.EC1_Phone2 LIKE '%param1%'
OR EME.EC2_Phone1 LIKE '%param1%'
OR EME.EC2_Phone2 LIKE '%param1%'
OR EME.EC3_Phone1 LIKE '%param1%'
OR EME.EC3_Phone2 LIKE '%param1%'
OR PREV.Prev_School_Phone LIKE '%param1%'
OR PREV.Prev_School_Fax LIKE '%param1%'
OR ST.Doctor_Phone LIKE '%param1%' 
order by ST.lastfirst

谢谢

1 个答案:

答案 0 :(得分:0)

一种方法是使用CROSS APPLY

Select DISTINCT 
    ST.SCHOOLID, 
    ST.STUDENT_NUMBER,
    ST.LASTFIRST, 
    ST.GRADE_LEVEL,
    C.Col -- Column name
FROM PS.STUDENTS ST
LEFT OUTER JOIN PS.U_GUARDIANS GA
    ON ST.DCID = GA.STUDENTSDCID
LEFT OUTER JOIN PS.U_DEMO DE
    ON ST.DCID = DE.STUDENTSDCID
LEFT OUTER JOIN PS.U_EMERGENCY EME
    ON ST.DCID = EME.STUDENTSDCID
LEFT OUTER JOIN PS.U_STUDENT_HISTORY PREV
    ON ST.DCID = EME.STUDENTSDCID
CROSS APPLY
(
    VALUES
    ('ST.Home_Phone', ST.Home_Phone),
    ('DE.STUDENT_CELL', DE.STUDENT_CELL),
    ('GA.G1_PHONE1', GA.G1_PHONE1),
    ('GA.G1_PHONE2', GA.G1_PHONE2),
    ('GA.G1_WORK_PHONE', GA.G1_WORK_PHONE),
    ('GA.G2_PHONE1', GA.G2_PHONE1),
    ('GA.G2_PHONE2', GA.G2_PHONE2),
    ('GA.G2_WORK_PHONE', GA.G2_WORK_PHONE),
    ('GA.G3_PHONE1', GA.G3_PHONE1),
    ('GA.G3_PHONE2', GA.G3_PHONE2),
    ('GA.G3_WORK_PHONE', GA.G3_WORK_PHONE),
    ('GA.G4_PHONE1', GA.G4_PHONE1),
    ('GA.G4_PHONE2', GA.G4_PHONE2),
    ('GA.G4_WORK_PHONE', GA.G4_WORK_PHONE),
    ('EME.EC1_Phone1', EME.EC1_Phone1),
    ('EME.EC1_Phone2', EME.EC1_Phone2),
    ('EME.EC2_Phone1', EME.EC2_Phone1),
    ('EME.EC2_Phone2', EME.EC2_Phone2),
    ('EME.EC3_Phone1', EME.EC3_Phone1),
    ('EME.EC3_Phone2', EME.EC3_Phone2),
    ('PREV.Prev_School_Phone', PREV.Prev_School_Phone),
    ('PREV.Prev_School_Fax', PREV.Prev_School_Fax),
    ('ST.Doctor_Phone', ST.Doctor_Phone)
) AS C(Col, Val)
WHERE C.Val LIKE '%param1%'
ORDER BY ST.lastfirst;

如果列类型不是同一类型(例如,一个是整数),则可能会产生错误,在这种情况下,您需要在交叉应用中将列转换为相同的类型。

此方法还将复制匹配的每列的行(例如,如果它匹配A列和B列,您将获得一行val1,val2,val3,val4,A和一行val1,val2, val3,val4,B)。如果这是一个问题,可以通过稍微调整交叉应用的方式来解决这个问题。

修改

作为示例数据的示例,运行此示例将显示查询应执行的操作:

DECLARE @T TABLE 
(
    SchoolID INT NOT NULL, Student_Number INT NOT NULL, LastFirst INT NOT NULL, Grade_Level INT NOT NULL,
    Home_Phone VARCHAR(255), Student_Cell VARCHAR(255), Phone1 VARCHAR(255), Phone2 VARCHAR(255), Phone3 VARCHAR(255),
    Phone4 VARCHAR(255), RandomColumnName VARCHAR(255), ABCDEFGHIJKL VARCHAR(255)
);

INSERT @T (SchoolID, Student_Number, LastFirst, Grade_Level, Home_Phone, Student_Cell, Phone1, Phone2, Phone3, Phone4, RandomColumnName, ABCDEFGHIJKL) VALUES
(1, 1, 1, 1, NULL, NULL, NULL, NULL, NULL, 'sdgfagagparam1sdasf', NULL, NULL),
(1, 1, 1, 2, NULL, NULL, NULL, NULL, 'asdfparam1sadf', NULL, NULL, NULL),
(1, 1, 1, 3, NULL, NULL, 'param1asdf', NULL, NULL, NULL, NULL, NULL),
(1, 1, 2, 1, NULL, NULL, NULL, NULL, NULL, NULL, 'adfparam1', NULL),
(1, 1, 2, 2, NULL, 'qqqparam1', NULL, NULL, NULL, NULL, NULL, NULL),
(1, 2, 1, 2, NULL, NULL, NULL, NULL, 'param1', NULL, NULL, 'param1'),
(2, 1, 1, 2, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);

SELECT DISTINCT 
    T.SchoolID, 
    T.Student_Number, 
    T.LastFirst, 
    T.Grade_Level, 
    C.Col
FROM @T AS T
CROSS APPLY
(
    VALUES
    ('Home_Phone', Home_Phone),
    ('Student_Cell', Student_Cell),
    ('Phone1', Phone1),
    ('Phone2', Phone2),
    ('Phone3', Phone3),
    ('Phone4', Phone4),
    ('RandomColumnName', RandomColumnName),
    ('ABCDEFGHIJKL', ABCDEFGHIJKL)
) AS C(Col, Val)
WHERE C.Val LIKE '%param1%';