MySQL基础查询-商城之类的网站,查询是最多的

本文详细介绍了SQL查询的基础语法及高级用法,包括字段选择、条件过滤、分组统计、排序与分页等功能,并通过实例展示了如何高效地进行数据检索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


     格式:
      select [字段列表]|* 
      from 表名
      where 搜索条件
      group by 分组字段  [having 子条件]
      order by 排序 asc | desc
      limit 分页参数


      顺序必须是这个顺序


 字段部分
    
    1)所有字段
      select * from 表名;
      select * from 库名.表名;
      
    2)部分字段
      select 字段1,字段2 from 表名;
      select 表名.字段1,表名.字段2 from 表名;
      
    3)查询10年后的年龄
      select uname,sex,age+10 from users;

    4)别名
      select uname,sex,age+10 xxoo from users;
      select uname,sex,age from users u;   
      
    5)列合并,合并年龄与性别
      select uname,concat(sex,',',age) from users;
      
      
    6)去除重复的字段,班级,性别
      select distinct classid,sex from users;
    
    7)在查询结果中无中生有添加一列
      select *,'天津校区' as '校区' from users;


 条件部分   
 
    8)php185的学生
     select * from users where classid='lamp185';
      
    9)php189的女生
     select * from users where classid='189' and sex='w';
      
    10)年龄大于20的学生
     select * from users where age > 20;
      
    11)年龄 在 30 到 40 的人
     select * from users where age>=30 and age<=40;
     select * from users where age between 30 and 40;
      
    12)age 不在 20 到 30 的人
     select * from users where age<20 or age>30;
     select * from users where age not between 20 and 30;
     
    13)php189班和php185班的女生
     select * from users where (classid='lamp189' or classid='lamp185') and sex='w';
      
    14)185班的男生和189班的女生
     select * from users where (classid='lamp185' and sex='m') or (classid='lamp189' and sex='w');
      
    15)找uid为 3,5,8,9,12 的人
     select * from users where uid in(3,5,8,9,12);
      
      找uid 不是 12,15,17 的人
     select * from users where uid not in(12,15,17);
      
    16)查询classid值不为NULL的人
select * from users where classid is not NULL;
      
    17)找名字为2个字的人
_ 匹配任意一个字符
     select * from users where uname like '__';
      
    18)找名字中以'李'开头的  
% 匹配任意个任意字符
     select * from users where uname like '李%';
      
    19)找名字中以'春'结尾的
     select * from users where uname like '%春';
    
    20)找包含'翔'的人
     select * from users where uname like '%翔%';
    
  分组查询  group by   分组伴随着统计  男生多少  女生多少
  
  统计函数:
      count(*) 统计个数   count(classid) 不会统计值为NULL的记录
      sum()    求和   比如符合条件的人的总年龄
      avg()    平均数
      max()  min()
      
    1)lamp185总共多少人
      select count(*) from users where classid='lamp185';
      
    2)每个班人数
      select classid,count(*) from users group by classid;

    3)最大年龄,最小年龄,平均年龄
      select max(age) from users;
      select min(age) from users;
      select avg(age) from users;
        
        //查找年龄最大的那个人
        select * from users where age=最大年龄;
        select * from users where age=(select max(age) from users);
        
    4)在平均年龄之上的人
      select * from users where age>平均年龄
      select * from users where age>(select avg(age) from users);
        
    5)每个班的男生,女生各多少人
      select classid,sex,count(*) from users group by classid,sex;
    
    6)每个班的男生女生各多少人,要求在20-30岁的
      select classid,sex,count(*) from users where age between 20 an 30 group by classid,sex;
    
    7)每个班多少人,只显示班级人数大于12的
      select classid,count(*) from users group by classid having count(*)>12;
    
    
  排序  order by 字段名   默认 asc升序    desc降序
  
    1)年龄从小到大排序
      select * from users order by age;
       
    2)年龄从大到小排序
      select * from users order by age desc;

    3)先女后男,年龄再小到大     先按性别排序,相同性别再按年龄排序
       select * from users order by sex,age
      先男后女,年龄从大到小
       select * from users order by sex desc,age desc;
       
    4)先按班级排序,再按年龄从大到小
       select * from users order by classid,age desc;
        
   
  分页 
    
    获取部分数据  limit m,n;
        m 代表跳过的记录数
        n 代表要显示的记录数
        
       1) select * from users limit 5;    跳过0条,显示5条记录
       2) select * from users limit 2,3;  跳过2条,显示3条记录
   
       条件: 每页显示5条记录
           第一页   limit 0,5
           第二页   limit 5,5
           第三页   limit 10,5
           第四页   limit 15,5
           第五页   limit 20,5
           
       分页公式:  第n页  limit (n-1)*5,5  
  
       1)年龄最大的3个人,(如果第3名和第4名年龄相同,取uid最大的)
         select * from users order by age desc,uid desc limit 3;
         
       2)188班女生年龄最小的3个人
         select * from users where classid='lamp188' and sex='w' order by age limit 3;
         
       3)女生最多的班级
         select classid,count(*) from users where sex='w' group by classid order by count(*) desc limit 1;
  
  
笛卡尔积
 
    
   内联
   
    // 用户的所有信息
    select * from student,details where student.uid=details.uid;
    select s.*,d.sex,d.age,d.tel from student s, details d where s.uid=d.uid;
    
    SELECT * FROM student s INNER JOIN details d  ON s.uid=d.uid;
    
    // 编号为5的学员信息
    select s.*,d.sex,d.age,d.tel from student s, details d where s.uid=d.uid and s.uid=5;
    
    
    select * from student,score where student.uid=score.uid; 
    
   左联
   
    // 学员的成绩
    select * from student LEFT JOIN score ON student.uid=score.uid;
   
   右联
    // 这些成绩属于谁
    select * from student RIGHT JOIN score ON student.uid=score.uid;
    


    A left join B   left join  C
  
    企业级应用   互联网级应用
    联查           坚决不能使用联查
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值