我下面的写法对吗?
DECLARE @tableID table(uID char(06))
INSERT INTO @tableID (
SELECT [USER_ID]
FROM BRS_M_USER
UNION
SELECT [USER_ID]
FROM BRS_M_USER_AUTH
UNION
SELECT [USER_ID]
FROM BRS_M_USER_USAGI
UNION
SELECT [USER_ID]
FROM BRS_M_USER_AUTH_USAGI
)
我用union取出不同的值,也不知道对不对 ,大家帮忙看看 谢谢
DECLARE @tableID table(uID char(06))
INSERT INTO @tableID (
SELECT [USER_ID]
FROM BRS_M_USER
UNION
SELECT [USER_ID]
FROM BRS_M_USER_AUTH
UNION
SELECT [USER_ID]
FROM BRS_M_USER_USAGI
UNION
SELECT [USER_ID]
FROM BRS_M_USER_AUTH_USAGI
)
我用union取出不同的值,也不知道对不对 ,大家帮忙看看 谢谢
DECLARE @tableID table(uID char(06))
INSERT INTO @tableID
SELECT [USER_ID]
FROM BRS_M_USER
UNION
SELECT [USER_ID]
FROM BRS_M_USER_AUTH
UNION
SELECT [USER_ID]
FROM BRS_M_USER_USAGI
UNION
SELECT [USER_ID]
FROM BRS_M_USER_AUTH_USAGI
我查询后那些结果要按升序排列,ORDER BY应该加在那一块
2,order by 对union之合集有效。
declare @t table(id int)insert @t
select 1 x
union all select 21 x
union all select 3 x
order by xselect * from @t
DECLARE @tableID table(uID char(06))INSERT INTO @tableID SELECT [USER_ID] FROM BRS_M_USER
UNION
SELECT [USER_ID] FROM BRS_M_USER_AUTH
UNION
SELECT [USER_ID] FROM BRS_M_USER_USAGI
UNION
SELECT [USER_ID] FROM BRS_M_USER_AUTH_USAGI
语句 order by 应加于最后一个查询
DECLARE @tableID table(uID char(06)) INSERT INTO @tableID Select * From (SELECT [USER_ID] FROM BRS_M_USER UNION SELECT [USER_ID] FROM BRS_M_USER_AUTH UNION SELECT [USER_ID] FROM BRS_M_USER_USAGI UNION SELECT [USER_ID] FROM BRS_M_USER_AUTH_USAGI) A Order by [USER_ID]
create table BRS_M_USER ([USER_ID] varchar(20))
create table BRS_M_USER_AUTH ([USER_ID] varchar(20))insert into BRS_M_USER
SELECT 'Lori001'
insert into BRS_M_USER
SELECT 'Lori002'insert into BRS_M_USER_AUTH
SELECT 'ghostmdx1001'insert into BRS_M_USER_AUTH
SELECT 'ghostmdx1002'
insert into BRS_M_USER_AUTH
SELECT 'ghostmdx1003'DECLARE @tableID table(uID char(20))
INSERT INTO @tableID
SELECT [USER_ID] FROM BRS_M_USER
UNION
SELECT [USER_ID] FROM BRS_M_USER_AUTH
select * from @tableID--ghostmdx1001
--ghostmdx1002
--ghostmdx1003
--Lori001
--Lori002
DECLARE @tableID table(uID char(20))
INSERT INTO @tableID
select [USER_ID] from (
SELECT top 1000 [USER_ID] FROM BRS_M_USER order by [USER_ID] desc)a
UNION
SELECT [USER_ID] FROM BRS_M_USER_AUTH
select * from @tableID--ghostmdx1001
--ghostmdx1002
--ghostmdx1003
--Lori001
--Lori002
DECLARE @tableID table(uID char(20))
INSERT INTO @tableID
SELECT [USER_ID] FROM BRS_M_USER
UNION
SELECT [USER_ID] FROM BRS_M_USER_AUTH
select * from @tableID
order by uID desc--Lori002
--Lori001
--ghostmdx1003
--ghostmdx1002
--ghostmdx1001