转载自https://github.com/Snailclimb/JavaGuide(添加小部分笔记)感谢作者!
题目来源于: 牛客题霸 - SQL 必知必会
检索数据#
select 用于从数据库中查询数据。
从 Customers 表中检索所有的 ID#
现有表 Customers 如下:
| cust_id |
|---|
| A |
| B |
| C |
编写 SQL 语句,从 Customers 表中检索所有的 cust_id。
答案:
select cust_id
from Customers;检索并列出已订购产品的清单#
表 OrderItems 含有非空的列 prod_id 代表商品 id,包含了所有已订购的商品(有些已被订购多次)。
| prod_id |
|---|
| a1 |
| a2 |
| a3 |
| a4 |
| a5 |
| a6 |
| a7 |
编写 SQL 语句,检索并列出所有已订购商品(prod_id)的去重后的清单。
答案:
select distinct prod_id
from OrderItems;知识点:distinct 用于返回列中的唯一不同值。
检索所有列#
现在有 Customers 表(表中含有列 cust_id 代表客户 id,cust_name 代表客户姓名)
| cust_id | cust_name |
|---|---|
| a1 | andy |
| a2 | ben |
| a3 | tony |
| a4 | tom |
| a5 | an |
| a6 | lee |
| a7 | hex |
需要编写 SQL 语句,检索所有列。














