Announcement

Collapse
No announcement yet.

Vererbung und JOIN

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

  • Vererbung und JOIN

    Hallo zusammen!

    Ich habe in meiner DB drei Tabellen, aus denen ich eine View erstellen möchte:

    articles
    id (int)

    books
    id (int)
    name (varchar)
    isbn (varchar)
    article_id (int) -> articles.id

    drinks
    id (int)
    name (varchar)
    color (varchar)
    article_id (int) -> articles.id
    Meine SELECT-Abfrage:

    [highlight=sql]SELECT
    articles.id,
    books.id AS bookid,
    books.name,
    books.isbn,
    drinks.id AS drinkid,
    drinks.name,
    drinks.color
    FROM
    articles
    JOIN books ON books.article_id = articles.id
    JOIN drinks ON drinks.article_id = articles.id[/highlight]

    Ergebnistabelle:
    id
    bookid
    name
    drinkid
    name
    color

    Die Spalte "name" würde ich aber gerne nur einmal haben. Wie muss ich dazu meine Abfrage verändern?

    Danke
    Zuletzt editiert von automatix1311; 15.12.2011, 22:26.

  • #2
    Hallo,
    Originally posted by automatix1311 View Post
    ...Die Spalte "name" würde ich aber gerne nur einmal haben. Wie muss ich dazu meine Abfrage verändern?
    Ähm... indem du dich für eine entscheidest und die andere im Select weglässt!?
    Wie wäre sonst deine Frage zu verstehen?

    Gruß Falk

    P.S.: Formatierung von SQL in Beiträgen
    Wenn du denkst du hast alle Bugs gefunden, dann ist das ein Bug in deiner Denksoftware.

    Quellcode ohne ein Mindestmaß an Formatierung sehe ich mir nicht an! Ich leiste keinen Privatsupport per Mail oder PN!

    Comment


    • #3
      Das Attribut "name" haben beide Tabellen. Eigentlich ist es eine gemeinsame Eigenschaft, die in der Tabelle "articles" stehen könnte. Aber jetzt steht sie in den Tabellen "books" und "drinks" und ch muss eine Abfrage schreiben, die am Ende folgende Result-Tabelle ergibt:

      article_id <- articles.id
      id (int) <- books.id bzw. drinks.id
      name (varchar) <- books.name bzw. drinks.name
      isbn (varchar) <- books.isbn; in den drinks-Zeilen entsprechend leer
      color (varchar) <- drinks.isbn; in den books-Zeilen entsprechend leer

      Wie kann man das erreichen?

      P.S. Danke für den Hinweis, geändert.

      Comment


      • #4
        Hallo,
        Originally posted by automatix1311 View Post
        ...Wie kann man das erreichen?
        z.B.mit einem UNION:
        [highlight=sql]
        SELECT b.article_id,
        b.id,
        b.name,
        b.isbn,
        null color
        FROM articles a
        JOIN books b ON b.article_id = a.id
        UNION
        SELECT d.article_id,
        d.id,
        d.name,
        null isbn,
        d.isbn color
        FROM articles a
        JOIN drinks ON d.article_id = a.id
        [/highlight]

        Gruß Falk
        Wenn du denkst du hast alle Bugs gefunden, dann ist das ein Bug in deiner Denksoftware.

        Quellcode ohne ein Mindestmaß an Formatierung sehe ich mir nicht an! Ich leiste keinen Privatsupport per Mail oder PN!

        Comment


        • #5
          Top!

          Gab einen kleinen Tipp-Fehler (hast das Alias beim JOIN vergessen). Hier nochmal der korrigierte Code:

          [highlight=SQL] SELECT b.article_id,
          b.id,
          b.name,
          b.isbn,
          NULL color
          FROM articles a
          JOIN books b ON b.article_id = a.id
          UNION
          SELECT d.article_id,
          d.id,
          d.name,
          NULL isbn,
          d.isbn color
          FROM articles a
          JOIN drinks d ON d.article_id = a.id[/highlight]

          Vielen Dank!
          Zuletzt editiert von automatix1311; 01.04.2012, 13:32.

          Comment

          Working...
          X