# Query liefert nicht das, was erwartet wird

**URL:** <https://forum.shopware.com/t/query-liefert-nicht-das-was-erwartet-wird/104183>\
**Category:** Shopware 6 (German)\
**Created:** [3. Juni 2024 um 14:15 UTC](https://forum.shopware.com/t/query-liefert-nicht-das-was-erwartet-wird/104183 "2024-06-03T14:15:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomas.kipp](https://avatars.discourse-cdn.com/v4/letter/t/b5e925/32.png) [@thomas.kipp](https://forum.shopware.com/u/thomas.kipp)\
**Post date:** [3. Juni 2024 um 14:15 UTC](https://forum.shopware.com/t/query-liefert-nicht-das-was-erwartet-wird/104183/1 "2024-06-03T14:15:27Z")

</div>

Hallo, folgendes Query sollte eigentlich für den Wert attribute\_names\_count zumindest eine 1 ausgeben. Ich bekomme aber durchweg eine 0 ausgegeben und frage mich, warum? Folgendes Query liefert, wie zu erwarten, für attribute\_names\_count durchweg eine 1…

```auto
SELECT COUNT(property_group_option_translation.name) AS attribute_names_count,property_group_option_id FROM 
property_group_option_translation
GROUP BY property_group_option_translation.property_group_option_id;

```

Folgendes Query tut das aber nicht. Sind die LEFT JOINS falsch aufgebaut, oder ist das LEFT JOIN an sich das Problem, ich weiß es nicht. Weiß jemand Rat?

```auto
SELECT
    product.id,product.product_number,
    COUNT(property_group_option_translation.name) AS attribute_names_count,
    IF(COUNT(DISTINCT property_group_option_translation.name) > 1, 'TRUE', 'FALSE') AS has_multiple_attribute_names
FROM
    product
LEFT JOIN product_property ON
    product_property.product_id = product.id
LEFT JOIN property_group_option ON 
    property_group_option.property_group_id = product_property.property_group_option_id
LEFT JOIN property_group_option_translation ON 
    property_group_option.id = property_group_option_translation.property_group_option_id
WHERE
    product.parent_id IS NOT NULL
GROUP BY
    product.id;

```

Das ist der Response:

---

<div class="post-metadata">

**Author:** ![Teddie](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/teddie/32/26013_2.png) [@Teddie](https://forum.shopware.com/u/Teddie)\
**Post date:** [12. August 2024 um 21:22 UTC](https://forum.shopware.com/t/query-liefert-nicht-das-was-erwartet-wird/104183/2 "2024-08-12T21:22:06Z")

</div>

Wäre tatsächlich gut gewesen, wenn du es mal in Worte gefasst hättest, was genau du erreichen willst. Ich vermute du willst die Anzahl an Eigenschaften pro Produkt anzeigen lassen?  
Dann reicht es auch so:

```auto
SELECT
    p.id,
    p.product_number,
    COUNT(pp.property_group_option_id) AS attribute_names_count
FROM
    product p
LEFT JOIN product_property pp ON
    pp.product_id = p.id
    
    WHERE
    p.parent_id IS NOT NULL
GROUP BY
    p.id;

```

Jedoch bin ich mir bei deinem has\_multiple\_attribute\_names unsicher… wenn deine Eigenschaften richtig gepflegt sind sollte da immer eine 1 kommen, wenn du den Wert „red“ in die jeweiligen Sprachen übersetzt hast.

Schreib mal was du vor hast…

---

<div class="post-metadata">

**Author:** ![thomas.kipp](https://avatars.discourse-cdn.com/v4/letter/t/b5e925/32.png) [@thomas.kipp](https://forum.shopware.com/u/thomas.kipp)\
**Post date:** [21. August 2024 um 10:11 UTC](https://forum.shopware.com/t/query-liefert-nicht-das-was-erwartet-wird/104183/3 "2024-08-21T10:11:03Z")

</div>

Das wollte ich erreichen und habe ich erreicht:

```auto
SET @languageID='2fbb5fe2e29a4d70aa5854ce7ce3e20b';
SELECT product.id,product.product_number,
COUNT(DISTINCT property_group_translation.name) AS attribute_names_count,
IF(COUNT(DISTINCT property_group_translation.name) > 1, 'TRUE', 'FALSE') AS has_multiple_attribute_names,property_group_translation.name
FROM product
LEFT JOIN product_property
ON product_property.product_id = product.id
LEFT JOIN property_group_option
ON product_property.property_group_option_id=property_group_option.id
LEFT JOIN property_group_translation ON property_group_option.property_group_id=property_group_translation.property_group_id
WHERE
product.parent_id IS NOT NULL
AND property_group_translation.language_id=UNHEX(@languageID)
GROUP BY
product.product_number,property_group_translation.name
HAVING
attribute_names_count>=1;

```

---

<div class="post-metadata">

**Author:** ![system](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/3X/2/9/29c7587ba660f00e4995d1743162782e5d6d8653.svg) [@system](https://forum.shopware.com/u/system)\
**Post date:** [20. September 2024 um 10:11 UTC](https://forum.shopware.com/t/query-liefert-nicht-das-was-erwartet-wird/104183/4 "2024-09-20T10:11:41Z")

</div>

Dieses Thema wurde automatisch 30 Tage nach der letzten Antwort geschlossen. Es sind keine neuen Antworten mehr erlaubt.
