Ankündigung

Einklappen
Keine Ankündigung bisher.

MYSQL Abfrage aus mehreren Tabellen --- Ich stehe auf dem Schlauch

Einklappen

Neue Werbung 2019

Einklappen
X
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • MYSQL Abfrage aus mehreren Tabellen --- Ich stehe auf dem Schlauch

    Hallo zusammen,

    ich programmiere schon länger mal mehr mal weniger aufwändige PHP Scripts, aber nur als Hobby, alles selbst beigebracht oder per Try&Error. Nun stehe ich aber auf dem Schlauch, bzw. komme alleine nicht weiter.

    Ich brauche für ein Script die Daten aus 3 Tabellen -> orders, orders_products und orders_products_properties

    Die Relationship zwischen orders und orders_products ist die orders_id, zwischen orders_products und orders_products_properties ist die orders_products_id.

    Die orders werden als erstes ausgelesen, da ich alle orders_id's benötige, die einen bestimmten orders_status haben. Wenn ich die orders_id's (als String) habe, lese ich die orders_products aktuell mit whereIn aus. Auch hier erstelle ich mir dann einen String und rufe damit die orders_products_properties ebenfalls per WhereIn aus. bei allen 3 Abfragen bastel ich mir dann ein Multi-Array mit dem ich dann weiter arbeite.

    Ich bin irgendwie der Meinung, dass dies auch besser zu bewerkstelligen ist, nur stehe ich absolut auf dem Schlauch.

    Vielleicht kann mir jemand auf die Sprünge helfen, muss auch keine fertige Lösung sein.

    LG und vielen Dank schonmal
    ven

  • #2
    Keine Ahnung wofür du drei Abfragen brauchst, mit einer Priese JOINs sollte das doch mit einer Abfrage machbar sein. Statt langer Erklärungen wäre es aber sinnvoller wenn du Datenbankstruktur und eine Hand voll Testdaten (als SQL-Code) posten würdest ...

    Kommentar


    • #3
      Hi,

      mit Joins bin ich nicht weiter gekommen, da ich so (mit meinem beschränktem Wissen) nur z.b alle Produkte zu einer Bestellung auslesen kann. Ich benötige aber alle Bestellungen mit dem orders_status = 2, die dazugehörigen Produkte und die dazu gehörigen Eigenschaften.

      Ich habe hier die Datenbankstruktur jedoch stark gekürzt. Habe nur die relevanten Daten drin gelassen, die ich dann auch im Array benötige.

      Code:
      CREATE TABLE `orders` (
        `orders_id` int(11) NOT NULL,
        `customers_name` varchar(64) NOT NULL DEFAULT '',
        `orders_status` int(5) NOT NULL DEFAULT '0',  
      ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
      Code:
      CREATE TABLE `orders_products` (
        `orders_products_id` int(11) NOT NULL,
        `orders_id` int(11) NOT NULL DEFAULT '0',
        `products_id` int(11) NOT NULL DEFAULT '0',
        `products_model` varchar(64) DEFAULT NULL,
        `products_name` varchar(255) NOT NULL DEFAULT ''
      ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
      Code:
      CREATE TABLE `orders_products_properties` (
        `orders_products_properties_id` int(10) UNSIGNED NOT NULL,
        `orders_products_id` int(10) UNSIGNED DEFAULT NULL,
        `products_properties_combis_id` int(10) UNSIGNED DEFAULT NULL,
        `properties_name` varchar(255) NOT NULL DEFAULT '',
        `values_name` varchar(255) NOT NULL DEFAULT '',
        `properties_price_type` varchar(8) NOT NULL DEFAULT '',
        `properties_price` decimal(16,4) NOT NULL DEFAULT '0.0000'
      ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
      Testdaten:

      Code:
      --
      -- Daten für Tabelle `orders`
      --
      INSERT INTO `orders` (`orders_id`,`customers_name`,`orders_status`) VALUES
      (411333,'Kunde_1', 2)
      INSERT INTO `orders` (`orders_id,`customers_name`,`orders_status`) VALUES
      (411334,'Kunde_2', 2)
      INSERT INTO `orders` (`orders_id,`customers_name`,`orders_status`) VALUES
      (411335,'Kunde_3', 2)
      --
      -- Daten für Tabelle `orders_products`
      --
      INSERT INTO `orders_products` (`orders_products_id`, `orders_id`, `products_id`, `products_model`, `products_name`) VALUES
      (33794, 411333, 1988, '0101988', 'MVP Disc Sports | Cell Starter Bag | V2');
      INSERT INTO `orders_products` (`orders_products_id`, `orders_id`, `products_id`, `products_model`, `products_name`) VALUES
      (33795, 411334, 775, '1200775', 'Axiom Discs | Insanity | Proton');
      INSERT INTO `orders_products` (`orders_products_id`, `orders_id`, `products_id`, `products_model`, `products_name`) VALUES
      (33796, 411334, 775, '1200775', 'Axiom Discs | Insanity | Proton');
      INSERT INTO `orders_products` (`orders_products_id`, `orders_id`, `products_id`, `products_model`, `products_name`) VALUES
      (33797, 411335, 1524, '1201524', 'Axiom Discs | Tenacity | Neutron');
      INSERT INTO `orders_products` (`orders_products_id`, `orders_id`, `products_id`, `products_model`, `products_name`) VALUES
      (33798, 411335, 870, '0200870', 'Latitude 64° | Maul | Opto');
      INSERT INTO `orders_products` (`orders_products_id`, `orders_id`, `products_id`, `products_model`, `products_name`) VALUES
      (33799, 411335, 2152, '1202152', 'Axiom Discs | Proxy | Electron Medium');
      INSERT INTO `orders_products` (`orders_products_id`, `orders_id`, `products_id`, `products_model`, `products_name`) VALUES
      (33800, 411335, 1096, '0201096', 'Latitude 64° | Cutlass | Opto');
      INSERT INTO `orders_products` (`orders_products_id`, `orders_id`, `products_id`, `products_model`, `products_name`) VALUES
      (33801, 411335, 910, '0100910', 'MVP Disc Sports | Photon | Fission | Elaine King Signature Series');
      INSERT INTO `orders_products` (`orders_products_id`, `orders_id`, `products_id`, `products_model`, `products_name`) VALUES
      (33802, 411335, 910, '0100910', 'MVP Disc Sports | Photon | Fission | Elaine King Signature Series');
      INSERT INTO `orders_products` (`orders_products_id`, `orders_id`, `products_id`, `products_model`, `products_name`) VALUES
      (33803, 411335, 2114, '0202114', 'Latitude 64° | Maul | Opto Air');
      INSERT INTO `orders_products` (`orders_products_id`, `orders_id`, `products_id`, `products_model`, `products_name`) VALUES
      (33804, 411335, 870, '0200870', 'Latitude 64° | Maul | Opto');
      --
      -- Daten für Tabelle `orders_products_properties`
      --
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74000, 33794, 25026, 'Farbe', 'Limone', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74001, 33795, 26512, 'Gewicht', '164g', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74002, 33795, 26512, 'Farbe', 'Rosarot', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74003, 33795, 26512, '#', 'A', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74004, 33796, 26510, 'Gewicht', '162g', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74005, 33796, 26510, 'Farbe', 'Rosarot', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74006, 33796, 26510, '#', 'A', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74007, 33797, 17110, 'Farbe', 'Blau', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74008, 33797, 17110, 'Gewicht', '169g', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74009, 33797, 17110, '#', 'A', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74010, 33798, 25561, 'Farbe', 'Hellblau', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74011, 33798, 25561, 'Gewicht', '177g', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74012, 33798, 25561, '#', 'A', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74013, 33799, 28854, 'Gewicht', '175g', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74014, 33799, 28854, '#', 'D', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74015, 33799, 28854, 'Farbe', 'Cosmic', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74016, 33800, 21857, 'Farbe', 'Gelb', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74017, 33800, 21857, 'Gewicht', '171g', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74018, 33800, 21857, '#', 'A', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74019, 33801, 25232, 'Farbe', 'Dunkelgrün', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74020, 33801, 25232, 'Gewicht', '171g', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74021, 33801, 25232, '#', 'A', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74022, 33802, 29394, 'Gewicht', '170g', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74023, 33802, 29394, 'Farbe', 'Rotorange', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74024, 33802, 29394, '#', 'A', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74025, 33803, 27332, 'Gewicht', '155g', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74026, 33803, 27332, '#', 'A', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74027, 33803, 27332, 'Farbe', 'Pink', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74028, 33804, 26361, 'Farbe', 'Hellblau', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74029, 33804, 26361, 'Gewicht', '170g', '', '0.0000');
      INSERT INTO `orders_products_properties` (`orders_products_properties_id`, `orders_products_id`, `products_properties_combis_id`, `properties_name`, `values_name`, `properties_price_type`, `properties_price`) VALUES
      (74030, 33804, 26361, '#', 'B', '', '0.0000');

      Kommentar


      • #4
        Hallo,
        probier mal ein SQL-Statement welches in etwa so aussieht:
        Code:
        SELECT orders.orders_id
             , orders.customers_name
             , oders_products.products_name
             , oders_products_properties.properties_price
        FROM  orders
            , oders_products
            , oders_products_properties
        WHERE orders.status = 2
        AND   oders_products.orders_id = orders.orders_id
        AND   oders_products_properties.orders_products_id = oders_products.orders_products_id;
        Gruss Dieter

        Kommentar


        • #5
          Zitat von BigRib Beitrag anzeigen
          Ich habe hier die Datenbankstruktur jedoch stark gekürzt. Habe nur die relevanten Daten drin gelassen, die ich dann auch im Array benötige.
          Kürzen ist schon ok, du solltest dann aber auch sicher stellen dass du keine Syntaxfehler drin hast (Tabellendefinition orders hat ein Komma zu viel und die Daten dazu keine Semikolon am Ende und zwei Backticks zu wenig) … Du kannst auch auf eine "Join-Menge" noch ein Join anwenden:

          Code:
          SELECT o.orders_id,
              o.customers_name,
              op.products_name,
              opp.properties_name,
              opp.values_name
          FROM orders as o LEFT JOIN orders_products as op ON o.orders_id =op.orders_id
            LEFT JOIN orders_products_properties as opp ON op.orders_products_id = opp.orders_products_id
          WHERE o.orders_status = 2
          ORDER BY o.customers_name,op.products_name,opp.properties_name
          Die Spaltennamen wären mir aber zu umständlich, aber das ist Geschmackssache.

          Zitat von dbreuer Beitrag anzeigen
          probier mal ein SQL-Statement welches in etwa so aussieht: […]
          Schlechte Idee, implizite Joins machen das ganze nur unübersichtlich.

          Kommentar


          • #6
            Schlechte Idee, implizite Joins machen das ganze nur unübersichtlich.
            Das ist finde ich Geschmackssache.Ich habe mich mit der ANSI Join Syntax bislang noch nicht wirklich anfreunden können. Ich verwende die nur, wenn es von aussen vorgeschrieben ist. Aber ich will hier jetzt keinen Glaubenskrieg vom Zaun brechen. Wenn jamand jetzt SQL lernt ist es wohl besser mit ANSI-Joins zu arbeiten.

            Kommentar


            • #7

              ENGINE=MyISAM verwende besser InnoDB, da MyIsam veraltet ist und immer die ganze Tabelle sperrt beim Einfügen und Ändern von Datensätzen. Ich würde also folgendes machen
              PHP-Code:
              CREATE TABLE `order` (
                `
              idunsigned int(11NOT NULL,
                `
              customer_idunsigned int(11NOT NULL,
                `
              statustinyint(3NOT NULL DEFAULT '0',  
              ENGINE=InnoDB DEFAULT CHARSET=utf8
              Die customer_id folgt der Relation zu den Kunden in der Tabelle customer mit der id. Das ist zwar ein join mehr aber auf Dauer besser.
              Schreibe immer die Tabellen als Singular, führe in den Spaltennamen nicht als Wiederholung den Tabellennamen auf, das ist unnötig.

              Kommentar


              • #8
                Hallöchen,

                wollte mich nur kurz melden, nicht das ihr denkt, dass ich mich vom Acker gemacht habe. Musste nur fix weg und komme erst am WE wieder, dann teste ich mal was Ihr geschrieben habt. Vielen Dank schonmal. Sorry dass ich beim kürzen Fehler eingebaut habe, war so nicht geplant. Die Spaltennamen sind vom Shophersteller vorgegeben ;D

                Kommentar


                • #9
                  Zitat von protestix Beitrag anzeigen
                  ENGINE=MyISAM verwende besser InnoDB, da MyIsam veraltet ist und immer die ganze Tabelle sperrt beim Einfügen und Ändern von Datensätzen. Ich würde also folgendes machen
                  PHP-Code:
                  CREATE TABLE `order` (
                  `
                  idunsigned int(11NOT NULL,
                  `
                  customer_idunsigned int(11NOT NULL,
                  `
                  statustinyint(3NOT NULL DEFAULT '0',
                  ENGINE=InnoDB DEFAULT CHARSET=utf8
                  Die customer_id folgt der Relation zu den Kunden in der Tabelle customer mit der id. Das ist zwar ein join mehr aber auf Dauer besser.
                  Schreibe immer die Tabellen als Singular, führe in den Spaltennamen nicht als Wiederholung den Tabellennamen auf, das ist unnötig.
                  Danke Dir für die Ratschläge, die Datenbank ist aber vom Shophersteller vorgegeben, ich baue mir nur ein eigenes Backend

                  Kommentar


                  • #10
                    Zitat von tk1234 Beitrag anzeigen
                    Kürzen ist schon ok, du solltest dann aber auch sicher stellen dass du keine Syntaxfehler drin hast (Tabellendefinition orders hat ein Komma zu viel und die Daten dazu keine Semikolon am Ende und zwei Backticks zu wenig) … Du kannst auch auf eine "Join-Menge" noch ein Join anwenden:

                    Code:
                    SELECT o.orders_id,
                    o.customers_name,
                    op.products_name,
                    opp.properties_name,
                    opp.values_name
                    FROM orders as o LEFT JOIN orders_products as op ON o.orders_id =op.orders_id
                    LEFT JOIN orders_products_properties as opp ON op.orders_products_id = opp.orders_products_id
                    WHERE o.orders_status = 2
                    ORDER BY o.customers_name,op.products_name,opp.properties_name
                    Danke so funktioniert es

                    Kommentar


                    • #11
                      Zitat von BigRib Beitrag anzeigen
                      Danke Dir für die Ratschläge, die Datenbank ist aber vom Shophersteller vorgegeben, ich baue mir nur ein eigenes Backend
                      Wenn die immer noch MyISAM verwenden, würde ich die Softwareware wechseln. Du kannst aber auch so problemlos auf InnoDB umsteigen ohne irgendwelche Nachteile zu haben, im Gegenteil bringt dir nur Vorteile.

                      Kommentar

                      Lädt...
                      X