我正在研究我的SQL,我有以下声明:
1 SELECT c.credit_display, t.title, t.provider
2 FROM credits c
3 INNER JOIN title t
4 ON c.vendor_id = t.vendor_id
5 WHERE c.position = 'Director' and t.provider = 'Premiere'
6 INTO OUTFILE '/var/tmp/file.csv'
7 FIELDS TERMINATED BY ','
我想知道关于标签的格式是否正确。什么时候缩进(例如,第三行应该是两个标签还是三个?)。
答案 0 :(得分:2)
这都是非常主观的。
由于INTO OUTFILE
和FIELDS
选项属于整个SELECT
语句,我可能不会在WHERE
子句下缩进它们,而是在最左边的位置(没有缩进)
由于您有多个WHERE
条件,因此您可以在新行上缩进每个条件。我会将相同的内容应用于SELECT
列和FROM
表。 ON
中的JOIN
子句在其适用的JOIN
ed表下方缩进。
SELECT
/* each column on its own line */
/* unless it is only 1 column */
c.credit_display,
t.title,
t.provider
FROM
credits c
INNER JOIN title t
/* ON clause indented beneath the join */
ON c.vendor_id = t.vendor_id
WHERE
/* each condition on its own line */
c.position = 'Director'
AND t.provider = 'Premiere'
/* This apply to the entire statement so I would not indent them
INTO OUTFILE '/var/tmp/file.csv'
/* You might indent this and other LINES TERMINATED BY options
beneath the OUTFILE line */
FIELDS TERMINATED BY ','
缩进是一个非常主观的问题,最好的办法是与你的团队就你如何缩进代码达成一致。
答案 1 :(得分:0)
这都是个人偏好(或公司编码标准,取决于您的环境)。我倾向于写这样的查询:
SELECT c.credit_display, t.title, t.provider
FROM credits c
INNER JOIN title t ON c.vendor_id = t.vendor_id
WHERE c.position = 'Director' AND t.provider = 'Premiere'
INTO OUTFILE '/var/tmp/file.csv'
FIELDS TERMINATED BY ','
查询的每个新行都对齐。缩进取决于它在代码中的位置以及使用的语言。但同样,这完全取决于个人偏好或公司标准。