常用経典sql文



1.SQL             ID 
insert into Table1(Name,des,num) values ('ltp','thisisbest',10);

select @@identity as 'Id'

2.   1 0       
select name,Sex=

  case Sex 

  when '1' then ' ' 

  when '0' then ' '

  end 

from Tablename

3.     
select a,b,c from Table1 where a IN (select a from Table2)

4.    、          
select a.title,a.username,b.adddate 

from tablename a,(select max(adddate) adddate from tablename where 

        tablename.title=a.title) b

5.          
SQL Server:Select Top 10 * From Tablename Order By NewID() 

Access:Select Top 10 * From Tablename Order By Rnd(ID)  

Rnd(ID)    ID       ,             ,       UserName) 

Select Top 10 * From  Order BY Rnd(Len(UserName)) 

MySql:Select * From  Order By Rand() Limit 10

6.             
select UserID from Accounts_Users

where UserName is not null

group by UserID

having count (*)>1

7.             
SELECT CategoryName,ProductName

FROM Categories LEFT JOIN Products 

ON Categories.CategoryID = Products.CategoryID;

8.        2 5       
select * from UserValue  where UserID between 2 and 5

9.      5    
Select * from TabSchedule where datediff(minute,getdate(),    )<5

10.              
SELECT DAY(DATEADD(dd, -DAY('2008-02-13'),DATEADD(mm, 1, '2008-02-13'))) 

        AS 'DayNumber'

11.       
Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as

12.        
1  select * from tablename where column1 like '[A-M]%'

         column       A-M     

2  select * from tablename where column1 like '[ABC]%'

         column       A  B  C   

3  select * from tablename where column1 like '[A-CG]%'

         column       A-C      G   

4  select * from tablename where column1 like '[^C]%'

         column        C   

   (   :like _):

         (_),          

select * from tablename where column1 like 'M_crosoft'

      :([ ] _ - %)

      []    ,  :

select * from tablename where column1 like '%[%]%'

13.     (     ,   :a,    :b)
select * into b from a where 1<>1 

 

select top 0 * into [b] from [a]

14.     (    ,   :a,    :b)
insert into b(Name,des,num) select Name,des,num from Table1;

15.     
   :

alter table Table1 add username varchar(30) not null default ''

   :

alter table Table1 alter column username varchar(40)

   : 

alter table Table1 drop column username

16.           
--     2006-11-1    23      21 

update Ad_Browse_20061101

set browsetime='2006-11-01 21'+

SUBSTRING(CONVERT(VARCHAR(30),browsetime,8),3,7)

where DATEPART(Hour,browsetime)=23