本文主要是介绍要求把user表内的phone列为'1386'开头的行全部放在前面,然后address列包含'北京'的全部放在后面,其余的放在中间。不能有重复行,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
面试时
要求把user表内的phone列为'1386'开头的行全部放在前面,
然后address列包含'北京'的全部放在后面,
其余的放在中间。
不能有重复行
-----
大概写了一下发现UNION 和UNION ALL的区别
UNION 会排序去重复
UNION ALL 不会排序不会去重复所以要自己写(排序要写在最后一个查询哪里)
----分割线-------------------
创建表
CREATE TABLE [dbo].[user]
(
[id] [int] NOT NULL,
[name] [nchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[sex] [nchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[phone] [nchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[address] [nchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[user] ADD CONSTRAINT [PK_user] PRIMARY KEY CLUSTERED ([id]) ON [PRIMARY]
GO
-----分割线----------------------------
插入数据
INSERT INTO dbo.[user]
( id, name, sex, phone, address )
VALUES ( 1, -- id - int
'张三', -- name - nchar(50)
N'男', -- sex - nchar(50)
N'13864587159', -- phone - nchar(50)
N'北京' -- address - nchar(50)
),
( 2, -- id - int
'李四 ', -- name - nchar(50)
N'女', -- sex - nchar(50)
N'12345678911', -- phone - nchar(50)
N'深圳' -- address - nchar(50)
),
( 3, -- id - int
'王五', -- name - nchar(50)
N'男', -- sex - nchar(50)
N'13868789211', -- phone - nchar(50)
N'湖北' -- address - nchar(50)
),
( 4, -- id - int
'陈六', -- name - nchar(50)
N'男', -- sex - nchar(50)
N'18947114561', -- phone - nchar(50)
N'广州' -- address - nchar(50)
),
( 5, -- id - int
'张三', -- name - nchar(50)
N'男', -- sex - nchar(50)
N'13864587159', -- phone - nchar(50)
N'北京' -- address - nchar(50)
),
( 6, -- id - int
'鲁七', -- name - nchar(50)
N'男', -- sex - nchar(50)
N'15872345134', -- phone - nchar(50)
N'深圳 ' -- address - nchar(50)
),
( 7, -- id - int
'赵九', -- name - nchar(50)
N'男', -- sex - nchar(50)
N'14725836911', -- phone - nchar(50)
N'北京' -- address - nchar(50)
),
( 8, -- id - int
'赵九', -- name - nchar(50)
N'男', -- sex - nchar(50)
N'14725836911', -- phone - nchar(50)
N'北京' -- address - nchar(50)
),
( 9, -- id - int
'熊十', -- name - nchar(50)
N'女', -- sex - nchar(50)
N'13486261341', -- phone - nchar(50)
N'深圳' -- address - nchar(50)
),
( 10, -- id - int
'杨十一', -- name - nchar(50)
N'男', -- sex - nchar(50)
N'13862384544 ', -- phone - nchar(50)
N'深圳' -- address - nchar(50)
)
----------分割线--------
--查询全部数据
--查询语句union
SELECT * FROM (SELECT * FROM dbo.[user] WHERE phone LIKE '%1386%') a
UNION
SELECT * FROM (SELECT * FROM dbo.[user] WHERE id NOT IN( SELECT id FROM dbo.[user] WHERE phone LIKE '%1386%' )AND id NOT IN
(SELECT id FROM dbo.[user] WHERE address LIKE '%北京%'))b
UNION
SELECT * FROM (SELECT * FROM dbo.[user] WHERE address LIKE '%北京%' )c
--查询语句UNION ALL
SELECT * FROM (SELECT * FROM dbo.[user] WHERE phone LIKE '%1386%') a
UNION ALL
SELECT * FROM (SELECT * FROM dbo.[user] WHERE id NOT IN( SELECT id FROM dbo.[user] WHERE phone LIKE '%1386%' )AND id NOT IN
(SELECT id FROM dbo.[user] WHERE address LIKE '%北京%'))b
UNION ALL
SELECT * FROM (SELECT * FROM dbo.[user] WHERE address LIKE '%北京%')c
(排序加 order by id)
发现他不会排序且不会去重复---查询语句
SELECT * FROM (SELECT * FROM dbo.[user] WHERE phone LIKE '%1386%') a
UNION ALL
SELECT * FROM (SELECT * FROM dbo.[user] WHERE id NOT IN( SELECT id FROM dbo.[user] WHERE phone LIKE '%1386%' )AND id NOT IN
(SELECT id FROM dbo.[user] WHERE address LIKE '%北京%'))b
UNION ALL
SELECT * FROM (SELECT * FROM dbo.[user] WHERE address LIKE '%北京%' AND phone NOT LIKE '%1386%' )c
这篇关于要求把user表内的phone列为'1386'开头的行全部放在前面,然后address列包含'北京'的全部放在后面,其余的放在中间。不能有重复行的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!