Distinct auf mehrere Felder

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Distinct auf mehrere Felder

    Hallo zusammen. Vielleicht stehe ich auf dem Schlauch oder bin aus der Übung - jedenfalls weiss ich gerade nicht, wie ich meine SQL-Query aufbauen soll.

    Ich habe ne Tabelle mit Adressdaten, dort kann man bis zu drei Städte eintragen.
    stadt1, stadt2, stadt3

    Nun möchte ich eine Liste aller Städte erstellen, in denen die Städte nur einmal auftauchen und alphabetisch sortiert sind.

    Kann ich DISTINCT auf stadt1, stadt2, stadt3 anwenden aber wie mache ich das mit dem ORDER?

    Habe auch schon sowas probiert - aber ohne Erfolg
    SELECT DISTINCT stadt1 AS stadt, stadt2 AS stadt, stadt3 AS stadt ORDER BY stadt ASC

    Pleae help!

    Danke,
    Andi

  • #2
    evt union und distinct
    oder in php mit array_unique
    Beantworte nie Threads mit mehr als 15 followups...
    Real programmers confuse Halloween and Christmas because OCT 31 = DEC 25

    Comment


    • #3
      PHP Code:
      SELECT
          stadt1
      FROM
          staedte
      UNION
      SELECT
          stadt2
      FROM
          staedte
      UNION
      SELECT
          stadt3
      FROM
          staedte
      ORDER BY 1 
      Gruss
      H2O

      Comment


      • #4
        Ja cool, mit UNION gehts!

        "(SELECT DISTINCT stadt1 AS stadt FROM $table) UNION (SELECT DISTINCT stadt2 AS stadt FROM $table) UNION (SELECT DISTINCT stadt3 AS stadt FROM $table) ORDER BY stadt"

        Merci,
        Andi

        Comment


        • #5
          Bei UNION muss du DISTINC nicht verwenden, das macht UNION schon automatisch.

          Comment


          • #6
            Stimmt! :-)
            Danke

            Comment

            Working...
            X