redbullmachtfit
Goto Top

MySQL zwei Tabellen zusammenführen

Hallo,
ich habe in MySQL eine Artikel-Tabelle in welcher alle Artikel gespeichert sind sowie eine art_preise-Tabelle, in welcher nur die Artikel aufgeführt sind, deren Preise geändert werden sollen. Teils sind die Artikel also in beiden Tabellen enthalten.
Ich benötige nun eine Ausgabe, in welcher mir ALLE Artikel erscheinen. Jeweils mit den Preisen aus der "normalen" Artikel-Tabelle ODER wenn der Artikel auch in art_preise vorhanden, NUR den Preisen aus art_preise.
Ich komme auf keinen grünen Zweig. Wie kann ich das am Besten lösen?

SELECT * FROM
(SELECT 'art_preise' AS quelle, ap.apr_artlfdnr, ap.apr_vkpreis_001, ap.apr_vkpreis_002, ap.apr_vkpreis_003, ap.apr_vkpreis_004,   
ap.apr_vkpreis_005, ap.apr_vkpreis_006, ap.apr_vkpreis_007, ap.apr_vkpreis_008, ap.apr_vkpreis_009
FROM art_preise ap
UNION DISTINCT
SELECT 'artikel' AS quelle, a.art_lfdnr, a.art_vkpreis_001, a.art_vkpreis_002, a.art_vkpreis_003, a.art_vkpreis_004,   
a.art_vkpreis_005, a.art_vkpreis_006, a.art_vkpreis_007, a.art_vkpreis_008, a.art_vkpreis_009
FROM artikel a) AS preise

Danke euch!

Content-Key: 275929

Url: https://administrator.de/contentid/275929

Ausgedruckt am: 29.03.2024 um 01:03 Uhr

Mitglied: ukulele-7
ukulele-7 29.06.2015 um 08:30:25 Uhr
Goto Top
Du suchst in etwa das hier:
SELECT	at.bezeichnung,
		( CASE WHEN ap.preis IS NOT NULL THEN ap.preis ELSE at.preis END ) AS preis
FROM	art_tabelle at
LEFT JOIN art_preise ap
ON		at.pk = ap.pk
Das kann man sicher auch anders schreiben aber dein Code verwirrt mich doch sehr face-smile
Mitglied: ukulele-7
ukulele-7 29.06.2015 um 08:34:33 Uhr
Goto Top
Oder auch:
SELECT	at.bezeichnung,
		ap.preis
FROM	art_tabelle at
INNER JOIN art_preise ap
ON		at.pk = ap.pk
UNION ALL
SELECT	at.bezeichnung,
		at.preis
FROM	art_tabelle at
WHERE NOT EXISTS (	SELECT	1
					FROM	art_preise ap
					WHERE	ap.pk = at.pk )
Mitglied: LianenSchwinger
Lösung LianenSchwinger 29.06.2015 aktualisiert um 09:37:27 Uhr
Goto Top
Hallo RedBullMachtFit, face-smile

hier noch ein Vorschlag.

SELECT a.art_lfdnr,
       NVL(ap.apr_vkpreis_001, a.art_vkpreis_001) AS art_preis_001,
       NVL(ap.apr_vkpreis_002, a.art_vkpreis_002) AS art_preis_002,
       NVL(ap.apr_vkpreis_003, a.art_vkpreis_003) AS art_preis_003,
       NVL(ap.apr_vkpreis_004, a.art_vkpreis_004) AS art_preis_004,
       NVL(ap.apr_vkpreis_005, a.art_vkpreis_005) AS art_preis_005,
       NVL(ap.apr_vkpreis_006, a.art_vkpreis_006) AS art_preis_006,
       NVL(ap.apr_vkpreis_007, a.art_vkpreis_007) AS art_preis_007,
       NVL(ap.apr_vkpreis_008, a.art_vkpreis_008) AS art_preis_008,
       NVL(ap.apr_vkpreis_009, a.art_vkpreis_009) AS art_preis_009
FROM artikel a
LEFT JOIN art_preise ap ON a.art_lfdnr = ap.apr_artlfdnr
ORDER BY a.art_lfdnr

Gruß Jörg
Mitglied: RedBullmachtfit
RedBullmachtfit 29.06.2015 um 09:01:50 Uhr
Goto Top
Super, danke! Habe es wie folgt gelöst:
SELECT a.art_nr, (CASE WHEN ap.apr_vkpreis_001 IS NOT NULL THEN ROUND(ap.apr_vkpreis_001, 2) ELSE ROUND(a.art_vkpreis_001, 2) END) AS preis1, 
(CASE WHEN ap.apr_vkpreis_002 IS NOT NULL THEN ROUND(ap.apr_vkpreis_002, 2) ELSE ROUND(a.art_vkpreis_002, 2) END) AS preis2, 
(CASE WHEN ap.apr_vkpreis_003 IS NOT NULL THEN ROUND(ap.apr_vkpreis_003, 2) ELSE ROUND(a.art_vkpreis_003, 2) END) AS preis3,
(CASE WHEN ap.apr_vkpreis_004 IS NOT NULL THEN ROUND(ap.apr_vkpreis_004, 2) ELSE ROUND(a.art_vkpreis_004, 2) END) AS preis4,
(CASE WHEN ap.apr_vkpreis_005 IS NOT NULL THEN ROUND(ap.apr_vkpreis_005, 2) ELSE ROUND(a.art_vkpreis_005, 2) END) AS preis5,
(CASE WHEN ap.apr_vkpreis_006 IS NOT NULL THEN ROUND(ap.apr_vkpreis_006, 2) ELSE ROUND(a.art_vkpreis_006, 2) END) AS preis6,
(CASE WHEN ap.apr_vkpreis_007 IS NOT NULL THEN ROUND(ap.apr_vkpreis_007, 2) ELSE ROUND(a.art_vkpreis_007, 2) END) AS preis7,
(CASE WHEN ap.apr_vkpreis_008 IS NOT NULL THEN ROUND(ap.apr_vkpreis_008, 2) ELSE ROUND(a.art_vkpreis_008, 2) END) AS preis8,
(CASE WHEN ap.apr_vkpreis_009 IS NOT NULL THEN ROUND(ap.apr_vkpreis_009, 2) ELSE ROUND(a.art_vkpreis_009, 2) END) AS preis9
FROM artikel a
LEFT JOIN art_preise ap
ON a.art_lfdnr = ap.apr_artlfdnr

Wenns noch einfacher geht bin ich für Ideen dankbar! face-smile
Mitglied: RedBullmachtfit
RedBullmachtfit 29.06.2015 um 09:02:39 Uhr
Goto Top
Ah ich war ein bisschen zu langsam! Danke für eure Tips, ich werde die Lösungen testen! Vielen Dank!
Mitglied: LianenSchwinger
Lösung LianenSchwinger 29.06.2015 aktualisiert um 09:37:24 Uhr
Goto Top
einfacher und kürzer, dann versuch das. face-smile
SELECT a.art_nr, 
       ROUND(NVL(ap.apr_vkpreis_001, a.art_vkpreis_001),2) AS art_preis_001, 
       ROUND(NVL(ap.apr_vkpreis_002, a.art_vkpreis_002),2) AS art_preis_002, 
       ROUND(NVL(ap.apr_vkpreis_003, a.art_vkpreis_003),2) AS art_preis_003, 
       ROUND(NVL(ap.apr_vkpreis_004, a.art_vkpreis_004),2) AS art_preis_004, 
       ROUND(NVL(ap.apr_vkpreis_005, a.art_vkpreis_005),2) AS art_preis_005, 
       ROUND(NVL(ap.apr_vkpreis_006, a.art_vkpreis_006),2) AS art_preis_006, 
       ROUND(NVL(ap.apr_vkpreis_007, a.art_vkpreis_007),2) AS art_preis_007, 
       ROUND(NVL(ap.apr_vkpreis_008, a.art_vkpreis_008),2) AS art_preis_008, 
       ROUND(NVL(ap.apr_vkpreis_009, a.art_vkpreis_009),2) AS art_preis_009 
FROM artikel a 
LEFT JOIN art_preise ap ON a.art_lfdnr = ap.apr_artlfdnr 
ORDER BY a.art_nr

G Jörg
Mitglied: RedBullmachtfit
RedBullmachtfit 29.06.2015 um 09:37:17 Uhr
Goto Top
So, hier die Finale Abfrage. Danke für eure Hilfe!

SELECT a.art_lfdnr, a.art_nr, 
       IFNULL(ap.apr_vkpreis_001, ROUND(a.art_vkpreis_001,2)) AS art_preis_001,
       IFNULL(ap.apr_vkpreis_002, ROUND(a.art_vkpreis_002,2)) AS art_preis_002,
       IFNULL(ap.apr_vkpreis_003, ROUND(a.art_vkpreis_003,2)) AS art_preis_003,
       IFNULL(ap.apr_vkpreis_004, ROUND(a.art_vkpreis_004,2)) AS art_preis_004,
       IFNULL(ap.apr_vkpreis_005, ROUND(a.art_vkpreis_005,2)) AS art_preis_005,
       IFNULL(ap.apr_vkpreis_006, ROUND(a.art_vkpreis_006,2)) AS art_preis_006,
       IFNULL(ap.apr_vkpreis_007, ROUND(a.art_vkpreis_007,2)) AS art_preis_007,
       IFNULL(ap.apr_vkpreis_008, ROUND(a.art_vkpreis_008,2)) AS art_preis_008,
       IFNULL(ap.apr_vkpreis_009, ROUND(a.art_vkpreis_009,2)) AS art_preis_009
FROM artikel a
LEFT JOIN art_preise ap ON a.art_lfdnr = ap.apr_artlfdnr
ORDER BY a.art_lfdnr
Mitglied: ukulele-7
ukulele-7 29.06.2015 um 09:38:06 Uhr
Goto Top
Oracle nvl() ist in MySQL IfNULL(), ich weiß aber nicht ob es nicht theoretisch auch 0 als Preis geben kann. Wenn dem so wäre ließe sich das mit CASE auch noch abfangen, musst du mal gucken.

Du kannst auch
ROUND((CASE WHEN ap.apr_vkpreis_001 IS NOT NULL THEN ap.apr_vkpreis_001 ELSE a.art_vkpreis_001 END),2) AS preis1
machen.
Mitglied: LianenSchwinger
LianenSchwinger 29.06.2015 um 09:42:24 Uhr
Goto Top
Laut MySQL Handbuch kennt MySql auch den NVL().

Und 0 ist nicht gleich NULL.

G Jörg
Mitglied: ukulele-7
ukulele-7 29.06.2015 um 09:48:49 Uhr
Goto Top
Zitat von @LianenSchwinger:
Und 0 ist nicht gleich NULL.
Darum ja, CASE kann mehr als nur auf NOT NULL prüfen, falls erforderlich.