单行查询与多行查询

未匹配的标注

你唯一应该把所有的 SQL 放在一行的时候,是当你 select 语句之后只有一个表达式,并且查询没有额外的复杂性:

-- Good
select * from users

-- Good
select id from users

-- Good
select count(*) from users

一旦你开始添加多个列,或者更复杂的查询时,将查询分散在多行之中,更容易阅读:

-- Good
select
    id,
    email,
    created_at
from users

-- Good
select *
from users
where email = 'example@domain.com'

-- Good
select
    user_id,
    count(*) as total_charges
from charges
group by user_id

-- Bad
select id, email, created_at
from users

-- Bad
select id,
    email
from users

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
讨论数量: 0
发起讨论 只看当前版本


暂无话题~