即使我认为这可能只是个人品味的问题,我想知道您更喜欢(以及为什么)在选择列表中使用逗号。
一开始:
select first_name
,last_name
,address
from people
或者结束:
select first_name,
last_name,
address
from people
我个人更喜欢在每行的开头使用逗号,尤其是当我处理比单列名称更复杂/长的值时。
我觉得这样做更舒服,因为我总是不明白新元素“开始”的时间/地点。
我很想知道你们是否有任何偏好(或特定场景)以便从多个角度查看这个主题。
最佳答案
它实际上以人类语言处理的方式很重要。 Joe Celko(SQL for Smarties 等书的作者)指出了这一点:
But be sure to end the line where the split occurs with language token that lets the reader know that there is more to come. This is why putting a comma at the front of the line is a bad practice; when you see a comma you know there is something coming after it. Likewise, when you see a semi-colon, you know it is a terminator. That lets the reader mentally close up that unit of code, “clear his buffers” and parse the next statement.
hackernoon 的 Felipe Hoffa 对前导逗号的使用进行了深入研究,发现强制使用它们的项目在 GitHub 上既不受欢迎也不成功:
https://hackernoon.com/winning-arguments-with-data-leading-with-commas-in-sql-672b3b81eac9
在前面使用 SQL 并没有错,但它不受欢迎并且会分散读者的注意力,因为它降低了语言的可读性。我不知道其他人,但是当任何标点符号(句点、逗号、分号)位于除括号或括号之外的行首时,我阅读代码并不容易。
关于SQL 可读性 : commas better at the end or at the beginning of select list?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35205891/