OceanBase 简单查询
2021-06-28 09:44 更新
简单查询指从 OceanBase 一个或多个表或视图中检索一个或多个列数据的操作,列的数量以及它们的数据类型和长度由表结构确定。而选择列表指的是 SELECT
关键字之后和 FROM
子句之前的表达式列表。
语法
SELECT 列名1,列名2,列名3,… FROM 表;
表名、字段名和关键字 SELECT
、FROM
不区分大小写。查询的最后可以跟上分号(;),多条 SQL 可以同时执行。您可以使用 SELECT
语句中的 Hint 将指令或提示传递给 OceanBase 数据库优化器。优化器使用 Hint 为语句选择执行计划。
示例
创建一张员工表 employee,并向列 employee_id、first_name、last_name、manager_id 和 salary 插入数据:
CREATE TABLE employee (
employee_id INT,
first_name VARCHAR(50),
last_name VARCHAR(50),
manager_id INT,
salary NUMERIC
);
INSERT INTO employee VALUES(111, 'DEL', 'FA BEN', 1, 1500);
INSERT INTO employee VALUES(112, 'AXEL', 'BELL', 1, 1000);
INSERT INTO employee VALUES(113, 'CRIS', 'RACHAR', 1, 1000);
-
查询部分列:
SELECT first_name, last_name, salary FROM employee;
+------------+-----------+--------+
| FIRST_NAME | LAST_NAME | SALARY |
+------------+-----------+--------+
| DEL | FA BEN | 1500 |
| AXEL | BELL | 1000 |
| CRIS | RACHAR | 1000 |
+------------+-----------+--------+
-
查询所有列:
SELECT * FROM employee;
+-------------+------------+-----------+------------+--------+
| EMPLOYEE_ID | FIRST_NAME | LAST_NAME | MANAGER_ID | SALARY |
+-------------+------------+-----------+------------+--------+
| 111 | DEL | FA BEN | 1 | 1500 |
| 112 | AXEL | BELL | 1 | 1000 |
| 113 | CRIS | RACHAR | 1 | 1000 |
+-------------+------------+-----------+------------+--------+
-
对列进行数学运算:
SELECT salary+100 FROM employee;
+------------+
| SALARY+100 |
+------------+
| 1600 |
| 1100 |
| 1100 |
+------------+
-
给列取别名:
SELECT salary*12 年薪 FROM employee;
+--------+
| 年薪 |
+--------+
| 18000 |
| 12000 |
| 12000 |
+--------+
-
字符串拼接:
SELECT first_name || '-' || last_name AS 姓名 FROM employee;
+-------------+
| 姓名 |
+-------------+
| DEL-FA BEN |
| AXEL-BELL |
| CRIS-RACHAR |
+-------------+
-
数据去重:
SELECT DISTINCT MANAGER_ID FROM employee;
+------------+
| MANAGER_ID |
+------------+
| 1 |
+------------+
CASE WHEN
语句:
SELECT salary, CASE WHEN salary >= 1000 then '高薪' WHEN salary >= 800 THEN '一般'
ELSE '继续努力' END AS typeFROM employee;
+--------+--------+
| SALARY | TYPE |
+--------+--------+
| 1500 | 高薪 |
| 1000 | 高薪 |
| 1000 | 高薪 |
+--------+--------+
以上内容是否对您有帮助:
更多建议: