table1:
id name
1 zhang
2 wang
期望结果:
id name
1 zhang
2 zhang
1 wang
2 wang
求最高效的sql 语句
------解决方案--------------------
我不敢说最高效
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-11-14 12:39:39
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[table1]
if object_id('[table1]') is not null drop table [table1]
go
create table [table1]([id] int,[name] varchar(5))
insert [table1]
select 1,'zhang' union all
select 2,'wang'
--------------开始查询--------------------------
select [table1].*
from [table1] CROSS JOIN (SELECT distinct id FROM table1) b
----------------结果----------------------------
/*
id name
----------- -----
1 zhang
2 wang
1 zhang
2 wang
*/