• Mysql常用查询


    IN/NOT IN操作符:查询满足(不满足)指定范围内的条件的记录。

    s_id为101和102的记录:

    MYSQL> SELECT s_id, s_name FROM fruits WHERE s_id IN (101,102);

    s_id不等于101和102的记录:

    MYSQL> SELECT s_id, s_name FROM fruits WHERE s_id NOT IN (101,102);

    BETWEEN AND/NOT BETWEEN AND查询: 查询指定范围内(外)的值(包括两个端点值),参数:范围的开始值和结束值。

    查询f_price在2~10之间的记录:

    MYSQL> SELECT f_name, f_price FROM fruits WHERE f_price BETWEEN 2 AND 10;

    LIKE操作符: 模糊查询,“%”匹配任意长度的字符,“_”匹配任意一个字符。

    查询以b开头的记录:

    MYSQL> SELECT f_name FROM fruits WHERE f_name LIKE 'b%';

    查询匹配ball的记录:

    MYSQL> SELECT f_name FROM fruits WHERE f_name LIKE 'bal_';

    NULL值查询: 如果某个字段设置为NULL(NULL不等于0,也不等于空字符串),则可使用IS NULL查询:

    查询f_name字段为NULL的记录:

    MYSQL> SELECT f_name FROM fruits WHERE f_name IS NULL;

    ORDER BY DESC/ASC: 降序/升序排序

    MYSQL> SELECT f_name FROM fruits ORDER BY f_name DESC;

    GROUP BY: 对数据按照某个字段或多个字段进行分组,和集合函数一起使用,MAX(),MIN(),COUNT(),SUM(),AVG()(以分组后的每一组为单位使用上述函数)

    GROUP BY 字段 HAVING 条件表达式;

    根据s_id对记录进行分组:

    MYSQL> SELECT s_id, COUNT(*) AS Total FROM fruits GROUP BY s_id;

    GROUP_COUNT: 显示每个分组中的字段值

    MYSQL> SELECT s_id, GROUP_COUNT(f_name) AS names FROM fruits GROUP BY s_id;
    # 显示每个分组中的所有f_name字段。

    HAVING 过滤分组

    MYSQL> SELECT s_id, s_name FROM friuts GROUP BY s_id HAVING COUNT(f_name) > 1;
    # 显示f_name数量大于1的分组信息。

    WITH ROLLUP: 显示分组后数据的所有记录

    MYSQL> SELECT s_id, COUNT(*) FROM fruits GROUP BY s_id WITH ROLLUP;

    多字段分组:

    MYSQL> SELECT * FROM fruits GROUP BY s_id, f_name;
    #首先根据s_id进行分组,再对每个分组里根据f_name进行分组。

    LIMIT 限制查询结果:

    LIMIT 位置偏移量(从哪一行开始), 行数
    
    MYSQL> SELECT * FROM fruits LIMIT 4;
    # 第一行为0,默认从第一行开始

    内连接查询(INNER JOIN):

    MYSQL> SELECT t_1.name, s_name FROM fruits, t_1 WHERE fruits.s_id = t_1.s_id;
    
    MYSQL> SELECT t_1.name, s_name FROM fruits INNER JOIN t_1 ON fruits.s_id = t_1.s_id;

    上述两个sql语句结果一致。

    注:如果一个连接查询中,涉及到的连个表是同一个表,这种查询成为自连接查询。

    MYSQL> SELECT f1.f_name, f1.s_id FROM fruits AS f1, fruits AS f2 WHERE f1.s_id = f2.s_id;

    左外连接查询(LEFT JOIN): 显示左表中的所有行,如果右表中没有对应的字段为NULL

    MYSQL> SELECT t_1.s_id, t_2.f_name FROM t_1 LEFT OUTER JOIN t_2 ON t_1.s_id = t_2.s_id;

    右外连接(RIGHT JOIN): 显示右表中的所有行。

    MYSQL> SELECT t_1.s_id, t_2.f_name FROM t_1 RIGHT OUTER JOIN t_2 ON t_1.s_id = t_2.s_id;

    符合条件连接查询

    MYSQL> SELECT t_1.s_id, t_2.f_name FROM t_1 INNER JOIN t_2 ON t_1.s_id = t_2.s_id AND t_1.f_name = 'abc' ORDER BY t_1.f_name;

    子查询(ANY,ALL,EXISTS):

    ANY:

    MYSQL> SELECT * FROM fruits WHERE s_id > ANY(SELECT s_id FROM t2);
    # 返回大于子查询结果中最小值的所有记录。

    ALL:

    MYSQL> SELECT * FROM fruits WHERE s_id > ALL(SELECT s_id FROM t2);
    # 返回大于子查询结果中最大值的所有记录。

    EXISTS:

    MYSQL> SELECT * FROM fruits WHERE EXISTS(SELECT s_id FROM t2);
    # 子查询是否有记录。

    NOT EXISTS:

    MYSQL> SELECT * FROM fruits WHERE NOT EXISTS(SELECT s_id FROM t2);
    # 如果子查询没有返回行则符合条件。

    IN/NOT IN:

    MYSQL> SELECT * FROM fruits WHERE s_id IN(SELECT s_id FROM t2);

    <、<=、=、>=、!=:

    MYSQL> SELECT * FROM fruits WHERE s_id = (SELECT s_id FROM t2 WHERE s_id = 1);

    合并查询结果

    UNION 不删除重复行,

    UNION ALL删除重复行

    MYSQL> SELECT s_id FROM fruits WHERE f_price < 9 UNION ALL SELECT s_id WHERE s_id IN(10,11);

    正则表达式查询(REGEXP)

    MYSQL> SELECT * FROM fruits WHERE f_name REGEXP '^b'; 
    # 查询f_name以b开头的记录。

    查询好友(好友关系表)(右外连接(RIGHT JOIN): 显示右表中的所有行。)

    SELECT tb_friend.FROM tb_friend RIGHT OUTER JOIN tb_user ON tb_friend.userid = tb_user.userId WHERE tb_friend.userid='18936958177'

    SELECT tb_user.* FROM friend RIGHT OUTER JOIN tb_user ON friend.userid = tb_user.userId WHERE friend.userid='123';

    查询群(右外连接(RIGHT JOIN): 显示右表中的所有行。)

    SELECT tb_group.* FROM tb_group_relation RIGHT OUTER JOIN tb_group ON tb_group_relation.groupid = tb_group.groupid WHERE tb_group_relation.userid='8355';
  • 相关阅读:
    举个手问个问题;
    c++字符串详解(转)
    Glide请求图片能携带Cookie的哟!
    Glide加载异常调试
    Notification中使用Glide
    manifest中读取<meta-data>
    Glide 魔法般的自定义扩展
    服务端报错死循环,无法输出错误页面问题
    android必须要进行为不同分辨率设备切图
    TextView无法通过setText设值
  • 原文地址:https://www.cnblogs.com/yanghj010/p/11841215.html
Copyright © 2020-2023  润新知