如何使用where条件从超过7个表中获取字段值

时间:2012-09-18 03:34:38

标签: sql

我有一个平面文件,其中包含一个移动号码列表及其状态为活动或非活动。所以我的工作是写一个SQL查询,这样我需要获取一个特定msisdn和我的7个表的列表必须得到msisdn的状态。所以有任何建议如何写。

我不能使用连接。

3 个答案:

答案 0 :(得分:1)

也许你会在此使用UNION

SELECT *
FROM
(
    SELECT mobileNo, moileStatus FROM table1
    UNION 
    SELECT mobileNo, moileStatus FROM table2
    UNION 
    SELECT mobileNo, moileStatus FROM table3
    UNION 
    SELECT mobileNo, moileStatus FROM table4
    UNION 
    SELECT mobileNo, moileStatus FROM table5
    UNION
    SELECT mobileNo, moileStatus FROM table6
    UNION 
    SELECT mobileNo, moileStatus FROM table7
) allMobile
WHERE allMobile.mobileNo = '' 
      -- OR something like that

答案 1 :(得分:1)

第1步:执行A BULK INSERT to Load the Text File

步骤2:从特定msisdn

的所有表中获取记录

步骤3:在特定msisdn

的步骤1和步骤2之间加入

伪代码

CREATE TABLE TempTable
(
 MSISDN INT,
 MobileNumber VARCHAR (50) NOT NULL,
 TheStatus varchar (10) NOT NULL
)

--Step 1: 
BULK INSERT TempTable FROM 'D:\InputMobileNumbers.txt' WITH (FIELDTERMINATOR = '","') 

-- Step 2:
INSERT INTO #TEMP 
SELECT * FROM 
(
SELECT MSISDN,MobileNumber FROM Table1 WHERE MSISDN = @MSISDN UNION All
SELECT MSISDN,MobileNumber FROM Table2 WHERE MSISDN = @MSISDN UNION All
.............................................................................
.............................................................................
SELECT MSISDN,MobileNumber FROM Table7 WHERE MSISDN = @MSISDN UNION All
)X

-- Step 3
SELECT tt.MSISDN,tt.MobileNumber,tt.TheStatus
FROM TempTable tt
JOIN #TEMP t ON tt.MSISDN = t.MSISDN
WHERE tt.MSISDN = @MSISDN


DROP TABLE #TEMP
DROP TABLE TempTable

答案 2 :(得分:0)

select t1.column1,t1.column2,...,t2.column4,..
from table1 t1 
left outer join table2 t2 on t1.column1=t2.column3
left outer join table2 t3 on t2.column1=t3.column2
.....
where t1.active=1 and t2.column2>0 ....
order by t1.column1,.....