简单的SQL语句整理

  1. -- 基本查询   
  2. select * from pet   
  3.   
  4. -- 列出指定的列   
  5. select name, owner form pet   
  6.   
  7. -- 直接进行算术运算,对字段起别名   
  8. select sin(1+2) as sin   
  9.   
  10. --where 条件   
  11. select * from pet where (birth>'1980' and species='dog'or species='bird'  
  12.   
  13. -- 对null 的条件   
  14. select * from pet where sex is not null  
  15.   
  16. -- 所有名字第四位是n 的宠物信息是   
  17. select * from pet where owner like '___n%'  
  18.   
  19.   
  20. -- 所有主人名叫gwen 或benny 的宠物   
  21. select * from pet where owner in ('gwen' , 'benny')   
  22.   
  23. -- 查询出生日期在90 年代是宠物,相当与 >= and   <=   
  24. select * from pet where birth between '1990' and '1999'  
  25.   
  26. -- 按主人姓名排序,相同的按宠物姓名倒序排列   
  27. select * from pet order by owner, name desc  
  28.   
  29. -- 查询性别为公的宠物,按生日倒序排列   
  30. select * from pet where sex='m' order by birth desc  
  31.   
  32. --char_lenngth() 返回的字符的长度,length() 返回字节长度   
  33. SELECT owner,length(owner),char_length(owner) FROM pet p;   
  34.   
  35.   
  36.   
  37. -- 列出养有宠物狗的人名   
  38. select distinct owner from pet where species='dog'  
  39.   
  40. -- 用两种方法查询出所有狗和猫的名字、出生年份、出生月份   
  41. select nameleft(birth,4) as year, mid(birth, 6, 2) as month from pet    
  42. where species='dog' or species='cat'  
  43.   
  44. select nameyear(birth) as yearmonth(birth) as month from pet    
  45. where species in('dog','cat')   
  46.   
  47. -- 查询所有名字中存在字母'e' 的人,将他们养的宠物按类别、年龄排序   
  48. select name, species, birth    
  49. from pet    
  50. where owner like '%e%'  
  51. order by species,birth desc  
  52.   
  53. -- 数字函数   
  54. select round(2.345,2), truncate(2.345,2), mod(323,5)   
  55.   
  56. -- 日期函数   
  57. select now(), curdate(), curtime()   
  58.   
  59. select adddate('2007-02-02', interval 31 day)   
  60.   
  61. -- 求出所有宠物的年龄   
  62. select name,birth,   
  63. truncate(datediff(now(),birth)/365,0) as age1,   
  64. year(now())-year(birth) - (dayofyear(birth)>dayofyear(now())) as age2   
  65. from pet   
  66.   
  67. -- 分组函数   
  68. select min(birth),max(birth),avg(birth),count(*),count(sex),   
  69. sum(birth)   
  70. from pet   
  71.   
  72. -- 每种宠物各有几只   
  73. select species,count(*)   
  74. from pet   
  75. group by species   
  76.   
  77. -- 查询年龄最大的宠物的信息   
  78. select * from pet where birth =   
  79.    (select max(birth) from pet)   
  80.   
  81. -- 每年各出生了几只宠物   
  82. select year(birth), count(*) from pet group by year(birth)   
  83.   
  84. -- 鸟和猫的性别比例   
  85. select species, sex, count(*)   
  86. from pet   
  87. where species in ('cat','bird')   
  88. group by species, sex   
  89.   
  90. -- 各种宠物年龄的和   
  91. select species, sum(truncate(datediff(now(),birth)/365,0)) as SumAge   
  92. from pet   
  93. group by species   
  94.   
  95. -- 数量大于1 的宠物种类   
  96. select species, count(*) as c   
  97. from pet   
  98. group by species   
  99. having c>=2   
  100.   
  101. -- 基本双表关联   
  102. select a.name,a.species, a.sex,b.date, b.type, b.remark   
  103. from pet a,event b   
  104. where a.name = b.name  
  105.   
  106. -- 查询宠物产仔时的年龄   
  107. select a.name, a.species,   
  108. truncate(datediff(b.date,a.birth)/365,0) as age   
  109. from pet a,event b   
  110. where a.name = b.name and b.type='litter'  
  111.   
  112. --90 年代出生的狗的事件列表   
  113. select a.name,birth,species,sex,date,type,remark   
  114. from pet a,event b   
  115. where a.name=b.name and birth between '1990' and '1999'  
  116. and species='dog'  
  117.   
  118. -- 活着的宠物按发生的事件类型分组,看各种事件发生的次数   
  119. select type, count(*)   
  120. from pet a, event b   
  121. where a.name=b.name and a.death is null  
  122. group by type   
  123.   
  124. -- 记录的事件数量超过1 条的宠物信息   
  125. select a.name,species,sex,count(*)   
  126. from pet a, event b   
  127. where a.name = b.name  
  128. group by b.name  
  129. having count(*)>=2   
  130.   
  131. -- 列出发生了两件事情的宠物的事件记录信息   
  132. select a.name,type,date,remark,b.species,b.sex,b.owner   
  133. from event a, pet b   
  134. where a.name=b.name and  
  135.    b.name in  
  136.    (   
  137. select name  
  138. from event   
  139. group by name  
  140. having count(*)=2   
  141.    )   
  142.   
  143.   
  144. -- 插入语句   
  145. insert into pet (name,species,birth)   
  146. values ('KKK','snake','2007-01-01');   
  147.   
  148. insert into pet   
  149. values ('KK','Diane','cat','f',null,null);   
  150.   
  151. insert into pet set name='k',owner='Benny'  
  152.   
  153.   
  154. -- 更新语句   
  155. update pet set species='snake',sex='f',birth=now()   
  156. where name='k'  
  157.   
  158. -- 将事件表中生日的日期,更新到pet 表中相应宠物的birth 字段   
  159. update pet a   
  160. set birth = (   
  161.              select date  
  162.              from event b   
  163.              where a.name=b.name and b.type='birthday'  
  164.          )   
  165. where a.name in (   
  166.                select name  
  167.                from event   
  168.                where type='birthday'  
  169.             )   
  170.   
  171.   
  172. -- 删除语句   
  173. delete from pet where name like 'k%'   
posted @ 2010-12-09 10:59  张 开  阅读(249)  评论(0编辑  收藏  举报