zoukankan      html  css  js  c++  java
  • java:mysql基础语法

    一,基础操作

    1,创建表

    CREATE TABLE `customers` (
    //设置customers_id为整数并为自增长 `customers_id`
    INT(11) PRIMARY KEY AUTO_INCREMENT, `last_name` VARCHAR(10) NOT NULL, `first_name` VARCHAR(10) NOT NULL, `dob` DATE, `phone` VARCHAR(12) )

    2,添加数据

    nsert into customers (
      last_name,first_name,dob,phone
    ) values (
      '', '','1965-01-01', '800-555-1211');

    3,使用LIKE操作符

       模式可以使用普通字符和两个通配符的组合指定:

        下划线字符(_)匹配指定上的一个字符

       百分号字符(%)匹配从指定位置开始的任意个字符。

       例:where first_name like'_o%'

    查询customers表中first_name这一列,这个列中有o字符的名。

    select *from customers where last_name like'%o%';

    4,使用in操作符

       使用in操作符从customers表中检索customer_id列的值为23、或5的记录。

        select * from customers where  customers_id in(2,3,5)

       not in的结果与使用in所查询的结果正好相反。

    select *from customers where customers_id in(2,3,5)

    如果指定的值列表中有一个为空值,那么not in就会返回false.,不会返回任何结果

    select *from customers where customers_id not in(2,3,4,5)

    5,使用逻辑操作符

    x and y              xy都为true时,返回true

    x or y               xy中有一个为true时,就返回true

    NOT x              如果xtrue则返回false,如果xfalse则返回true

    :从customers表中检索dob列大于197011日并且小于200011日的客户:

    select *from customers where dob>'1970-1-1' and dob<'2000-1-1';

    customers表中检索dob列大于201211日或者customers_id列大于3的行:

     select * from customers where dob> '2012-01-01'  or customers_id>3

    理解操作符的优先等级

    如果在一个sql语句中同时使用ANDOR操作符,那么AND的优先级要高于OR,而比较操作符的优先级

    高于AND,当然我们可以使用圆括号来改变优先级的顺序。

    7,使用ORDER BY子句对行进行排序

          order by子句用于对从数据库检索出的行进行排序,order by子句可以指定一列或多列(查询结果会根据这些列进行排序)

     而且必须位于FORMWHERE子句(如果提供WHERE子句)之后。    

       例:查询出products表中的数据,以价格进行排序(正序,倒序)

    select *from products where price order by price;
    select *from products where price order by price desc;

    8,使用between操作符

    例:从customers表中检索customers_id列的值在13之间的记录:

    select *from customers where customers_id between 1 and 3

    当然not between会返回相反的行,between也可以检索在二个sql语句之间的记录,当然要求sql语句

    查询出的结果要与类型匹配,为整型。

    : 查询价格在50-200之间的产品:

    select *from products where price between 10 and 25;

    9,多表查询

    内连接  join

    例:同时查看产品名称和产品类别二个表中的信息。

    select p.product_id, p.name,
               t.name as type, 
                     p.description,
             p.price     
        from   products p   join  product_types t
                 on p.product_type_id = t.product_type_id;

    二,函数与关键词

    1,在关联子查询中使用EXISTSNOT EXISTS

    EXISTS操作符用于检查子查询所返回的行的存在性。

    语法:where exists (sql语句)

     例:使用EXISTS查询负责管理其它员工的员工记录。

    select employee_id, concat(last_name,'',first_name) name from employees ee
         where  exists 
               (select employee_id from employees e where e.manager_id=ee.employee_id);

    由于EXISTS只是检查子查询返回行的存在性,所以查询不必返回一列,可以只返回一个常量值。这样提高查

    询的性能

    select employee_id, concat(last_name,'',first_name) name from employees ee
         where  exists 
               (select 1 from employees e where e.manager_id=ee.employee_id);

    2,Limit函数

    LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数。LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初始记录行的偏移量是 0(而不是 1)

    例: 

    SELECT * FROM table LIMIT 5,10;   检索记录行 6-15
    为了检索从某一个偏移量到记录集的结束所有的记录行,可以指定第二个参数为 -1SELECT * FROM table LIMIT 95,-1;  检索记录行 96-last.
    如果只给定一个参数,它表示返回最大的记录行数目: 
    SELECT * FROM table LIMIT 5;    检索前 5 个记录,换句话说,LIMIT n 等价于 LIMIT 0,n。

    3,Row()  CURDATE()插入当前时间

    NOW()函数以`'YYYY-MM-DD HH:MM:SS'返回当前的日期时间,可以直接存到DATETIME字段中。

    CURDATE()以’YYYY-MM-DD’的格式返回今天的日期,可以直接存到DATE字段中。

    CURTIME()以’HH:MM:SS’的格式返回当前的时间,可以直接存到TIME字段中。

    例:

    insert into customers(dob) values (now())

    4,AVG()函数

      AVG(x)函数用于计算x的平均值。

      例:计算产品的平均价格,products表中的price列作为参数传递给AVG()函数;

    select avg(price) from products; 

    5,count()函数

          用于计算一个查询所返回的行数。

    例:计算products表中的行数:

    select count(product_id) from products;

    6,MAX()MIN()

      用于计算x的最大值和最小值。

    select max(price),min(price) from products;

    7,SUM(x)函数

      计算并返回x中的所有值之和,

    例:计算products表中price列之和。

    select sum(price) from products  

    8,使用GROUP BY对行进行分组

        例如:查询products表中不同类型的产品的平均价格。

    select  product_type_id, avg(price) 平均价格 from 
    products p group by p.product_type_id;

    注:1、不能在where函数中使用聚合函数。

    2、如果查询中包含一个聚合函数,而所选择的列并不在聚合函数中,

    那么这些列就必须在GROUUP BY子句中。

    9,使用HAVING子句过滤行分组

       HAVING子句可以用于过滤分组,它可以放在GROUP BY子句之后,

       用于对分组后进行过滤。

    例:查询平均价格高于20块的产品类型。

     select product_type_id from products 
    group by product_type_id having avg(price)>20
  • 相关阅读:
    容器跨主机网络通信学习笔记(以Flannel为例)
    Kubernetes控制器Job和CronJob
    记一次使用Flannel插件排错历程
    Kubernetes控制器Deployment
    Kubernetes如何通过StatefulSet支持有状态应用?
    react18 来了,我 get 到...
    gojs 实用高级用法
    vuecli3 vue2 保留 webpack 支持 vite 成功实践
    calibre 报错 This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. 解决
    unable to recognize "*.yaml": no matches for kind "RoleBinding" in version "rbac.authorization.k8s.io/v1beta1"
  • 原文地址:https://www.cnblogs.com/dybe/p/8059562.html
Copyright © 2011-2022 走看看