美文网首页
java -通用查询sql语句

java -通用查询sql语句

作者: 走停2015_iOS开发 | 来源:发表于2020-12-29 16:11 被阅读0次
  • 简单查询语句(DQL)
    语法格式:
    select 字段1,字段2,字段3,.....from 表名;
 //*代表所有字段
 select *from 表名;

 select name, id  from t_user;
 select name, id*12  from t_user; 可以进行计算

//给查询列起别名 不会修改数据库
 select name as title, id  from t_user; 
 select name as` title`, id  from t_user;
//as可以省略加空格
select name  title, id  from t_user; 
 提示:
      1.任何一条sql语句以  ;  结尾
       2.sql语句不区分大小写

  • 条件查询
    语法格式
    select 字段1 字段2 from 表名 where 条件;
    提示:
    1.and:优先级高 or:优先级低
    2.or 等同于 in
    3.模糊查询like %:代表任意多个字符 _:代表任意一个字符
select name from t_user where name = 'LiLei';
+-------+
| name  |
+-------+
| LiLei |
+-------+

select * from t_user where name = 'LiLei'
+----+-------+-----+--------+
| id | name  | age | gender |
+----+-------+-----+--------+
|  1 | LiLei |  18 |      1 |
+----+-------+-----+--------+

select * from t_user where age>17;
+----+----------+-----+--------+
| id | name     | age | gender |
+----+----------+-----+--------+
|  1 | LiLei    |  18 |      1 |
|  5 | WeiHua   |  32 |      0 |
|  6 | ZhangWei |  25 |      1 |
|  7 | Ann      |  36 |      0 |
|  8 | Lisa     |  19 |      0 |
|  9 | ZhangWei |  18 |      1 |
+----+----------+-----+--------+

select *from t_user where id  between 1 and 5;  相当于 [1~5]
+----+-----------+-----+--------+
| id | name      | age | gender |
+----+-----------+-----+--------+
|  1 | LiLei     |  18 |      1 |
|  2 | HanMeimei |  17 |      0 |
|  3 | Lucy      |  17 |      0 |
|  4 | Lili      |  16 |      0 |
|  5 | WeiHua    |  32 |      0 |
+----+-----------+-----+--------+

select *from t_user where content is not  null;
+----+------+-----+--------+---------+
| id | name | age | gender | content |
+----+------+-----+--------+---------+
|  8 | Lisa |  19 |      0 | ceshi   |
+----+------+-----+--------+---------+

//俩个语句效果一样
select *from t_user where name='Lisa' or name ='ZhangWei';
select *from t_user where name in('Lisa','ZhangWei');
+----+----------+-----+--------+---------+
| id | name     | age | gender | content |
+----+----------+-----+--------+---------+
|  8 | Lisa     |  19 |      0 | ceshi   |
|  9 | ZhangWei |  18 |      1 | NULL    |
|  6 | ZhangWei |  25 |      1 | NULL    |
+----+----------+-----+--------+---------+

//名字中包含n的
select *from t_user where name like '%n%';
+----+-----------+-----+--------+---------+
| id | name      | age | gender | content |
+----+-----------+-----+--------+---------+
|  2 | HanMeimei |  17 |      0 | NULL    |
|  6 | ZhangWei  |  25 |      1 | NULL    |
|  7 | Ann       |  36 |      0 | NULL    |
|  9 | ZhangWei  |  18 |      1 | NULL    |
+----+-----------+-----+--------+---------+

//第二个字母为u的名字
select *from t_user where name like '_u%';
+----+------+-----+--------+---------+
| id | name | age | gender | content |
+----+------+-----+--------+---------+
|  3 | Lucy |  17 |      0 | NULL    |
+----+------+-----+--------+---------+

//特殊 名字中有下划线的
select *from t_user where name like '%_%' ;//不行
只能让 _ 转义 
select *from t_user where name like '%\_%'; ok
+----+-------+-----+--------+---------+
| id | name  | age | gender | content |
+----+-------+-----+--------+---------+
| 10 | Ka_te |  17 |      1 | NULL    |
+----+-------+-----+--------+---------+

相关文章

  • java -通用查询sql语句

    简单查询语句(DQL)语法格式:select 字段1,字段2,字段3,.....from 表名; 条件查询语法格式...

  • java历程1

    今天第一天参加正式笔试。失败。 递归。 五星数JAVA运算。 JAVA工厂模式。 SQL语句基础语句。 SQL多表查询。

  • Spring Data JPA问题集锦(一)

    -------采用SQL语句查询JPA导致BigInteger cannot be cast to java.la...

  • sqlserver,oracle,mysql分页查询通用sql

    摘要: sqlserver,oracle,mysql分页查询通用sql1:mysql通用查询sql /* sql:...

  • Java自学-JDBC 查询

    在JDBC中使用ResultSet查询SQL语句 执行查询SQL语句 步骤 1 : 查询语句 executeQue...

  • mysql数据库查询语句

    1.简单的查询基本表的SQL语句 (1)查询语句 (2)查询语句 Student表的删除SQL语句: 选课表的操作...

  • SQL查询语句

    常用SQL查询语句 一、简单查询语句 1. 查看表结构 SQL>DESC emp; 2. 查询所有列 SQL>SE...

  • 搜索

    直接sql查询 用sql语句like查询,复杂的用sql语句拼接。 视图和存储过程查询 简化程序,提高执行效率,维...

  • 数据库-sql基础应用&元素获取

    一.sql介绍 结构化的查询语言,关系型数据库中通用的一种命令。 使用SQL_MODE规范SQL语句的语法标准。 ...

  • SqlServer查询表空间占用情况

    查询sql语句

网友评论

      本文标题:java -通用查询sql语句

      本文链接:https://www.haomeiwen.com/subject/uudfoktx.html