DISTINCT problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • questionit
    Contributor
    • Feb 2007
    • 553

    #1

    DISTINCT problem

    If i have more than 1 fields, how would i use DISTINCT then

    For example

    SELECT house_id, house_name

    If i put distinct with 2nd field like:
    SELECT house_id, DISTINCT house_name

    i get error

    any help?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by questionit
    If i have more than 1 fields, how would i use DISTINCT then

    For example

    SELECT house_id, house_name

    If i put distinct with 2nd field like:
    SELECT house_id, DISTINCT house_name

    i get error

    any help?
    Try [CODE=sql]SELECT DISTINCT (house_name), house_id from ..[/CODE]

    Comment

    • questionit
      Contributor
      • Feb 2007
      • 553

      #3
      Hi

      why can i not use DISTINCT with the 2nd field in SELECT clause?
      Why in examples, it always appeat with the first SELECT field only ?


      Originally posted by r035198x
      Try [CODE=sql]SELECT DISTINCT (house_name), house_id from ..[/CODE]

      Comment

      • mwasif
        Recognized Expert Contributor
        • Jul 2006
        • 802

        #4
        Use GROUP BY instead

        [CODE=mysql]SELECT house_id, house_name...
        GROUP BY house_name[/CODE]

        Comment

        • mwasif
          Recognized Expert Contributor
          • Jul 2006
          • 802

          #5
          DISTINCT applies to all the columns in SELECT, you can not apply DISTINCT on one column and leave the others.

          Comment

          Working...