Vinzenz Mai: schweres Problem...

Beitrag lesen

Hallo Lavender

SELECT * FROM x.products, y.products_to_categories

Heißen Deine Tabellen x.products oder products, y.products_to_categories oder products_to_categories? Wolltest Du mit Aliasnamen arbeiten? Davon gehe ich mal aus:

WHERE x.products_status = '1'
AND y.categories_id = '22'
AND x.products_id = y.products_id
ORDER BY x.products_date_added DESC
LIMIT 0,9

Es sollen nur Artikel angezeigt werden, die auch in der zweiten Tabelle vorhanden sind (bei welcher auch noch unterschieden werden muss, ob der Wert zum zugehörigen Artikel 22 ist...

[code lang=sql]SELECT
    p.products_id,                   -- einmal sollte reichen
    p.products_quantity,
    p.products_date_added,
    p.products_status,
    pc.categories_id
FROM products p                      -- Aliasname p für Tabelle products
INNER JOIN products_to_categories pc -- Aliasname pc für Verknüpfungstabelle
ON p.products_id = pc.products_id    -- explizite Join-Syntax
WHERE p.product_status = 1           -- bnur Produkte mit Status 1
    AND pc.categories = 22           -- nur solche aus der Kategorie 22
ORDER BY p.products_date_added DESC  -- wie von Dir gewünscht sortiert
LIMIT 0, 9                           -- Ergebnismenge beschränken
[/ code]

sollte mit Deinen Ausgangsdaten

products

products_id|products_quantity|products_date_added|products_status
1          |       100       |     22.12.01      |       1
2          |       100       |     23.12.01      |       1
3          |       100       |     24.12.01      |       1
4          |       100       |     21.12.01      |       1
5          |       100       |     25.12.01      |       1

products_to_categories

products_id    | categories_id
1              |        21
2              |        22
3              |        22
4              |        22
5              |        23

das von Dir gewünschte Ergebnis liefern

products_id|products_quantity|products_date_added|products_status |categories_id
--------------------------------------------------------------------------------
3          |       100       |     24.12.01      |       1        | 22
2          |       100       |     23.12.01      |       1        | 22
4          |       100       |     21.12.01      |       1        | 22

Freundliche Grüße

Vinzenz