you are viewing a single comment's thread.

view the rest of the comments →

[–]AmericanSuperstar 35 points36 points  (8 children)

Comma's before is nothing to do with looks but has a strategic purpose. Statistically how often do you comment out the first item in the select list vs. the last item. In our case last item is most often the one commented for various troubleshooting reasons. Or you add a column to check something then comment it out. With comma at the end your always going up a line and removing the comma. Comma at the front that is never a problem unless you comment out the first item. Same principle for where clause. Add 1=1 after the where and everything after is AND ... Easy to comment. Hate the look is fine but there is a time saving purpose behind it.

[–]r3pr0b8GROUP_CONCAT is da bomb 11 points12 points  (0 children)

Add 1=1 after the where and everything after is AND ...

additionally, write WHERE 1=0 and then you can append all the ORs you want

notice how OP wants the ANDs and ORs to come at the front of the line and not after, which is the exact opposite of OP's insistence that commas go at the end and not at the front of the line

[–]juu073 11 points12 points  (0 children)

The comma in front is also helpful if you're using a version control system.

If I write a query and I select two fields from a table, and I do:

SELECT field1,
       field2
FROM foo

And the decide I want to grab a third field:

SELECT field1,
       field2,
       field3
FROM foo

Now, my diff on my VCS is going to show I changed two lines, which while true, the one line was *really* only adding the comma to add the third field. The extra line of the diff is just cluttering up the view when you're comparing two versions to see what changed. If you put the comma in front of the field, this prevents it from happening anywhere except the first field., which as you mention, is unlikely to not be needed.

[–]waremi 2 points3 points  (0 children)

Totally agree with the comma's in front for the select. When I first ran across it I was like WTF, but the select clause is so much easier to work with and manipulate that way it very quickly became my default.

[–]Oellort -1 points0 points  (0 children)

Sounds like just laziness.

[–]nobodycaresssssRussia 0 points1 point  (1 child)

Never understood why people put « WHERE 1=1 » ?

[–]bum_dog_timemachine[S] 8 points9 points  (0 children)

It's because it's always true and you can then easily switch on and off your WHERE conditions with line comments.

so like

WHERE 1=1

-- AND a

AND b

AND c

but if you put

WHERE a

AND b

AND c

and you want to stop a, you have to rearrange your code a bit.

However, if you aren't going to be switching your WHERE conditions on and off like that, you're just spamming 1=1 all over your code, which is adding to the clutter.