Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

SQL Pivot & UnPivot

create table students (
  name varchar(25),
  class varchar(25),
  grade int
)
insert into students values ('ÕÅÈý','ÓïÎÄ',20)
insert into students values ('ÕÅÈý','Êýѧ',90)
insert into students values ('ÕÅÈý','Ó¢Óï',50)
insert into students values ('ÀîËÄ','ÓïÎÄ',81)
insert into students values ('ÀîËÄ','Êýѧ',60)
insert into students values ('ÀîËÄ','Ó¢Óï',90)
select * from students
pivot(
max(grade)
FOR [class] IN ([ÓïÎÄ],[Êýѧ],[Ó¢Óï])
) AS pvt
/*
ÀîËÄ    81    60    90
ÕÅÈý    20    90    50
*/
--=========================================================================
create table students (
  name varchar(25),
  ÓïÎÄ int,
  Êýѧ int,
  Ó¢Óï int
)
GO
INSERT INTO students values ('ÀîËÄ',81,60,90)
INSERT INTO students values ('ÕÅÈý',20,90,50)
select *
from
students
unpivot
(
grade
for class in
([ÓïÎÄ],[Êýѧ],[Ó¢Óï])
) AS upvt
/*
ÀîËÄ    81    ÓïÎÄ
ÀîËÄ    60    Êýѧ
ÀîËÄ    90    Ó¢Óï
ÕÅÈý    20    ÓïÎÄ
ÕÅÈý    90    Êýѧ
ÕÅÈý    50    Ó¢Óï
*/


Ïà¹ØÎĵµ£º

sql²»³£¼ûÓï¾ä»ã×Ü

1¡¢join
A±íµÄÖ÷¼üÊÇ×÷ΪB±íµÄÍâ¼ü¡£ÔÚ²éѯµÄʱºò£¬¿ÉÒÔͨ¹ý²»Í¬µÄjoin½«AºÍB±íÁ´½ÓÆðÀ´£¬´Ó¶øµÃµ½²»Í¬µÄ²éѯ½á¹û¡£
* JOIN: Èç¹û±íÖÐÓÐÖÁÉÙÒ»¸öÆ¥Å䣬Ôò·µ»ØÐÐ
* INNER JOIN: Èç¹ûÁ½¸ö±íÖÐÓÐÆ¥ÅäµÄ£¬Ôò·µ»ØÐР         
* LEFT JOIN: ¼´Ê¹ÓÒ±íÖÐûÓÐÆ¥Å䣬Ҳ´Ó×ó± ......

SQL like×Ó¾äµÄÁíÒ»ÖÖʵÏÖ·½·¨,ËٶȱÈlike¿ì


SQL like×Ó¾äµÄÁíÒ»ÖÖʵÏÖ·½·¨,ËٶȱÈlike¿ì(ת)
Ò»°ãÀ´ËµÊ¹ÓÃÄ£ºý²éѯ£¬´ó¼Ò¶¼»áÏëµ½LIKE
select * from table where a like '%×Ö·û%'
Èç¹ûÒ»¸öSQLÓï¾äÖÐÓöà¸ö likeÄ£ºý²éѯ£¬²¢ÇҼǼÌõÊýºÜ´ó£¬ÄÇËÙ¶ÈÒ»¶¨»áºÜÂý¡£
ÏÂÃæÁ½ÖÖ·½·¨Ò²¿ÉʵÏÖÄ£ºý²éѯ£º
select * from table where patindex('%×Ö·û%',a)>0 ......

ÓÃÃüÁîÐÐÖ´ÐÐSQL½Å±¾Óï·¨½âÎö

osql   ʵÓù¤¾ß  
  osql   ʵÓù¤¾ßʹÄúµÃÒÔÊäÈë   Transact-SQL   Óï¾ä¡¢ÏµÍ³¹ý³ÌºÍ½Å±¾Îļþ¡£¸ÃʵÓù¤¾ßͨ¹ý   ODBC   Óë·þÎñÆ÷ͨѶ¡£  
   
  Óï·¨  
  osql  
          [-?]   |  
&nb ......

²éѯ´æ´¢¹ý³ÌµÄ²ÎÊýÐÅÏ¢µÄSQLÓï¾ä

declare @SchemaName nvarchar(50)
set @SchemaName='dbo'
declare @CommandName nvarchar(50)
set @CommandName='spName'
SELECT
                DB_NAME() AS [PROCEDURE_CATALOG],
         ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ