,=,18;selectid,name,"/> ,=,18;selectid,name,">

 >  기사  >  데이터 베이스  >  MySQL에는 어떤 데이터 쿼리 문이 있나요?

MySQL에는 어떤 데이터 쿼리 문이 있나요?

王林
王林앞으로
2023-05-26 14:16:203005검색

    1. 기본 개념(쿼리문)

    ①기본문

    1. "테이블 이름에서 * 선택;", —테이블의 모든 데이터를 쿼리할 수 있습니다.
    2. table name;", - 테이블에서 지정된 필드의 데이터를 쿼리할 수 있습니다.
    3. "테이블 이름에서 고유한 필드 이름을 선택합니다.", - 테이블의 데이터를 중복 없이 쿼리할 수 있습니다.
    4. "쿼리 조건이 있는 테이블 이름에서 필드 이름을 선택하세요.", —조건에 따라 테이블에 지정된 필드의 데이터를 쿼리할 수 있습니다. , c3d8bb965e4d2158a2e34cca99b96e7f;

    (2)命令:select 7d63c9f8ce32ff1ae4915f31bebead74 from a26d98d33123a70024fa8ba5642906c6;

    2、去重查询(distinct)

    命令:select distinct 7d63c9f8ce32ff1ae4915f31bebead74 from a26d98d33123a70024fa8ba5642906c6

    3、排序查询(order by)

    升序:asc

    降序:desc

    降序排列命令:select dfb08b8fe164362ddbbdb0005b7a6721 from a26d98d33123a70024fa8ba5642906c6 order by dfb08b8fe164362ddbbdb0005b7a6721 desc

    不加desc一般默认为升序排列

    4、分组查询(group by)

    命令:select 162131b8562e13ae131483a4c9d25bb6, Sum(score) from a26d98d33123a70024fa8ba5642906c6 group by 162131b8562e13ae131483a4c9d25bb6

    假设现在又有一个学生成绩表(result)。要求查询一个学生的总成绩。我们根据学号将他们分为了不同的组。

    命令:

    select id, Sum(score) 
    from result 
    group by id;

    现在有两个表学生表(stu)和成绩表(result)。

    5.等值查询

    当连接运算符为“=”时,为等值连接查询。

    现在要查询年龄小于20岁学生的不及格成绩。

    select stu.id,score 
    from stu,result 
    where stu.id = result.id and age < 20 and score < 60;

    等值查询效率太低

    6.外连接查询

    ①语法

    select f1,f2,f3,....
    from table1 left/right outer join table2
    on 条件;

    ②左外连接查询,例如

    select a.id,score
    from
      (select id,age from stu where age < 20) a (过滤左表信息)
    left join
      (select id, score from result where score < 60) b (过滤右表信息)
    on a.id = b.id;

    左外连接就是左表过滤的结果必须全部存在。右表中如果没有与左表过滤出来的数据匹配的记录,那么就会出现NULL值

    ③右外连接查询,例如

    select a.id,score
    from
      (select id,age from stu where age < 20) a (过滤左表信息)
    right join
      (select id, score from result where score < 60) b (过滤右表信息)
    on a.id = b.id;

    右外连接就是左表过滤的结果必须全部存在

    7.内连接查询

    ①语法

    select f1,f2,f3,....
    from table1 inter join table2
    on 条件;

    ②例如

    select a.id,score
    from
      (select id,age from stu where age < 20) a (过滤左表信息)
    inner join
      (select id, score from result where score < 60) b (过滤右表信息)
    on a.id = b.id;

    8.合并查询

    在图书表(t_book)和图书类别表(t_bookType)中

    ①.union

    使用union关键字是,数据库系统会将所有的查询结果合并到一起,然后去掉相同的记录;

    select id 
    from t_book  
     union 
    select id 
    from t_bookType;

    ②.union all

    使用union all,不会去除掉重复的记录;

    select id 
    from t_book  
     union all 
    select id 
    from t_bookType;

    위 내용은 MySQL에는 어떤 데이터 쿼리 문이 있나요?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

    성명:
    이 기사는 yisu.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제