learn_oracle_notebook/README.md

37 lines
1.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# learn_oracle_notebook
Oracle 学习笔记
## 查询 Select
>
> 查询语句
当涉及到数据库查询时,子查询和表关联是非常常见的操作。让我通过一些简单的例子,并对每个例子进行简要的讲解来帮助你完善笔记。
### 子查询
#### 关联子查询
```sql
SELECT *
FROM employees
WHERE department_id IN
(SELECT department_id
FROM departments
WHERE location_id = 1700);
```
这个例子演示了一个关联子查询。内部的子查询先根据条件从departments表中选择特定的部门ID然后外部的主查询根据这些部门ID从employees表中检索相关的员工信息。这种方式可以帮助我们根据内部查询的结果来过滤外部查询的数据。
### 表关联
#### 左连接
```sql
SELECT customers.name, orders.order_date
FROM customers
LEFT JOIN orders ON customers.id = orders.customer_id;
```
这个例子演示了左连接,它返回了所有的顾客信息以及与之关联的订单信息。即使某些顾客没有订单,也会返回顾客的信息。这在需要显示所有主体数据的情况下非常有用。