Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts

    DISTINCT question

    Which of these is correct...

    Option #1:
    Code:
    	$q1 = "SELECT DISTINCT (article)

    Option #2:
    Code:
    	$q1 = "SELECT DISTINCT article

    Option #3:
    It doesn't matter...


    Sincerely,


    Debbie

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,900
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Did you run a test to see if there was any difference between #1 and #2?

    Did you receive any errors? If not, then both are "correct".

    I have never seen any examples that used the parens and I have never used them, but I just ran a test and got the same results, so the answer is #3;

  • #3
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by FishMonger View Post
    Did you run a test to see if there was any difference between #1 and #2?

    Did you receive any errors? If not, then both are "correct".
    I didn't see any difference, but just because things run doesn't mean I'm not doing things the wrong way!!


    Quote Originally Posted by FishMonger View Post
    I have never seen any examples that used the parens and I have never used them, but I just ran a test and got the same results, so the answer is #3;
    In retrospect, I guess I am used to seeing SQL *without* the parentheses, but for some reason this time I was thinking of DISTINCT as a function, and so thought I had to wrap the column in parentheses...

    Just wanted to make sure I wasn't writing code that might blow up!!

    Sincerely,


    Debbie

  • #4
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    Don't use the brackets because when you select more than one column and are in the habit of using the brackets you may incorrectly think they should be there

    Code:
    SELECT DISTINCT (columnA), columnB, columnC
    FROM
      sometable
    doesn't apply distinct on column A but on all three columns at once. and the use of the brackets may lead you to believe the DISTINCT is only applied to columnA

  • Users who have thanked guelphdad for this post:

    doubledee (05-26-2014)

  • #5
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by guelphdad View Post
    Don't use the brackets because when you select more than one column and are in the habit of using the brackets you may incorrectly think they should be there

    Code:
    SELECT DISTINCT (columnA), columnB, columnC
    FROM
      sometable
    doesn't apply distinct on column A but on all three columns at once. and the use of the brackets may lead you to believe the DISTINCT is only applied to columnA

    Very good point!!

    Thanks,


    Debbie


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •