提交 49348e55 编写于 作者: M Mars Liu

format

上级 9d53b62d
...@@ -30,25 +30,34 @@ from employee as l ...@@ -30,25 +30,34 @@ from employee as l
### select 与 group by 不匹配 ### select 与 group by 不匹配
```postgresql ```postgresql
select id, name, dept, max(salary) from employee group by dept; select id, name, dept, max(salary)
from employee
group by dept;
``` ```
### group by 不对 ### group by 不对
```postgresql ```postgresql
select id, name, dept, max(salary) from employee group by dept, id, name; select id, name, dept, max(salary)
from employee
group by dept, id, name;
``` ```
### group by 不对 ### group by 不对
```postgresql ```postgresql
select id, name, dept, max(salary) from employee group by dept, id, name having salary = max(salary); select id, name, dept, max(salary)
from employee
group by dept, id, name
having salary = max(salary);
``` ```
### 结构错误 ### 结构错误
```postgresql ```postgresql
select id, name, dept, max(salary) from employee where salary=max(salary) group by dept; select id, name, dept, max(salary)
from employee
where salary = max(salary)
group by dept;
``` ```
...@@ -3,10 +3,11 @@ ...@@ -3,10 +3,11 @@
现有员工表 现有员工表
```postgresql ```postgresql
create table employee( create table employee
id serial primary key , (
name text, id serial primary key,
dept text, name text,
dept text,
salary money salary money
) )
``` ```
...@@ -16,9 +17,11 @@ create table employee( ...@@ -16,9 +17,11 @@ create table employee(
## 答案 ## 答案
```postgresql ```postgresql
select id, name, dept, salary select id, name, dept, salary
from employee from employee
where salary > (select max(salary) from employee where dept='sale') where salary > (select max(salary)
from employee
where dept = 'sale')
``` ```
## 选项 ## 选项
...@@ -26,27 +29,29 @@ where salary > (select max(salary) from employee where dept='sale') ...@@ -26,27 +29,29 @@ where salary > (select max(salary) from employee where dept='sale')
### A ### A
```postgresql ```postgresql
select id, name, dept, salary select id, name, dept, salary
from employee from employee
where dept = 'sale' where dept = 'sale'
group by dept having salary > max(salary) group by dept
having salary > max(salary)
``` ```
### B ### B
```postgresql ```postgresql
select l.id, l.name, l.dept, l.salary select l.id, l.name, l.dept, l.salary
from employee as l join employee as r on l.salary > max(r.salary) from employee as l
where r.dept = 'sale' join employee as r on l.salary > max(r.salary)
where r.dept = 'sale'
group by r.dept group by r.dept
``` ```
### C ### C
```postgresql ```postgresql
select id, name, dept, salary select id, name, dept, salary
from employee from employee
having salary > (select max(salary) from employee where dept='sale') having salary > (select max(salary) from employee where dept = 'sale')
``` ```
......
...@@ -18,26 +18,36 @@ create table orders ...@@ -18,26 +18,36 @@ create table orders
## 答案 ## 答案
```postgresql ```postgresql
select id, product_id, order_date, quantity, customer_id from orders where date = $1 offset $2 limit 100; select id, product_id, order_date, quantity, customer_id
from orders
where date = $1
offset $2 limit 100;
``` ```
## 选项 ## 选项
### 缺少 limit ### 缺少 limit
```postgresql ```postgresql
select id, product_id, order_date, quantity, customer_id from orders where date = $1 offset $2; select id, product_id, order_date, quantity, customer_id
from orders
where date = $1
offset $2;
``` ```
### 缺少 offset
### 缺少 offset
```postgresql ```postgresql
select id, product_id, order_date, quantity, customer_id from orders where date = $1; select id, product_id, order_date, quantity, customer_id
from orders
where date = $1;
``` ```
### 结构不对 ### 结构不对
```postgresql ```postgresql
select id, product_id, order_date, quantity, customer_id from orders where date = $1 and offset $2 and limit 100 ; select id, product_id, order_date, quantity, customer_id
from orders
where date = $1 and
offset $2 and limit 100;
``` ```
...@@ -18,13 +18,14 @@ create table node ...@@ -18,13 +18,14 @@ create table node
## 答案 ## 答案
```postgresql ```postgresql
with recursive t(id, pid, content) as (select id, pid, content with recursive t(id, pid, content) as (
from node select id, pid, content
where id = $1 from node
union all where id = $1
select node.id, node.pid, node.level union all
from node select node.id, node.pid, node.level
join t on node.id = t.pid) from node
join t on node.id = t.pid)
select node.id, node.pid, content select node.id, node.pid, content
from node from node
join t on node.id = t.id; join t on node.id = t.id;
...@@ -35,13 +36,14 @@ from node ...@@ -35,13 +36,14 @@ from node
### 没有递归定义 ### 没有递归定义
```postgresql ```postgresql
with t as (select id, pid, content with t as (
from node select id, pid, content
where id = $1 from node
union all where id = $1
select node.id, node.pid, node.level union all
from node select node.id, node.pid, node.level
join t on node.id = t.pid) from node
join t on node.id = t.pid)
select node.id, node.pid, content select node.id, node.pid, content
from node from node
join t on node.id = t.id; join t on node.id = t.id;
...@@ -53,7 +55,7 @@ from node ...@@ -53,7 +55,7 @@ from node
select node.id, node.pid, node.content select node.id, node.pid, node.content
from node from node
join node as p on node.pid = p.id join node as p on node.pid = p.id
where id=$1; where id = $1;
``` ```
### 子查询无法处理递归问题 ### 子查询无法处理递归问题
...@@ -61,5 +63,5 @@ where id=$1; ...@@ -61,5 +63,5 @@ where id=$1;
```postgresql ```postgresql
select node.id, node.pid, node content select node.id, node.pid, node content
from node as t from node as t
where t.pid = (select id from t where id=t.pid) where t.pid = (select id from t where id = t.pid)
``` ```
\ No newline at end of file
...@@ -5,11 +5,14 @@ ...@@ -5,11 +5,14 @@
## 答案 ## 答案
```postgresql ```postgresql
create function gen(start integer, stop integer) returns setof integer as $$ create function gen(start integer, stop integer)
begin returns setof integer as
for idx in start .. stop loop $$
return next idx; begin
end loop; for idx in start .. stop
loop
return next idx;
end loop;
end; end;
$$ language plpgsql; $$ language plpgsql;
``` ```
...@@ -19,11 +22,13 @@ $$ language plpgsql; ...@@ -19,11 +22,13 @@ $$ language plpgsql;
### A ### A
```postgresql ```postgresql
create function gen(start integer, stop integer) returns integer as $$ create function gen(start integer, stop integer) returns integer as
begin $$
for idx in start .. stop loop begin
return idx; for idx in start .. stop
end loop; loop
return idx;
end loop;
end; end;
$$ language plpgsql; $$ language plpgsql;
``` ```
...@@ -31,11 +36,14 @@ $$ language plpgsql; ...@@ -31,11 +36,14 @@ $$ language plpgsql;
### B ### B
```postgresql ```postgresql
create function gen(start integer, stop integer) returns integer as $$ create function gen(start integer, stop integer)
begin returns integer as
for idx in start .. stop loop $$
yield idx; begin
end loop; for idx in start .. stop
loop
yield idx;
end loop;
end; end;
$$ language plpgsql; $$ language plpgsql;
``` ```
...@@ -43,12 +51,15 @@ $$ language plpgsql; ...@@ -43,12 +51,15 @@ $$ language plpgsql;
### C ### C
```postgresql ```postgresql
create function gen(start integer, stop integer) returns setof integer as $$ create function gen(start integer, stop integer)
begin returns setof integer as
for idx in start .. stop loop $$
return idx; begin
idx += 1; for idx in start .. stop
end loop; loop
return idx;
idx += 1;
end loop;
end; end;
$$ language plpgsql; $$ language plpgsql;
``` ```
...@@ -56,11 +67,14 @@ $$ language plpgsql; ...@@ -56,11 +67,14 @@ $$ language plpgsql;
### D ### D
```postgresql ```postgresql
create function gen(start integer, stop integer) returns setof integer as $$ create function gen(start integer, stop integer)
begin returns setof integer as
for idx in start .. stop loop $$
select idx; begin
end loop ; for idx in start .. stop
loop
select idx;
end loop;
end; end;
$$ language plpgsql; $$ language plpgsql;
``` ```
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册