# Hauptbild einer Variante mittels SQL ermitteln

**URL:** <https://forum.shopware.com/t/hauptbild-einer-variante-mittels-sql-ermitteln/68917>\
**Category:** Programmierung\
**Created:** [12. August 2020 um 11:30 UTC](https://forum.shopware.com/t/hauptbild-einer-variante-mittels-sql-ermitteln/68917 "2020-08-12T11:30:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_MikeB](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/_mikeb/32/7856_2.png) [@\_MikeB](https://forum.shopware.com/u/_MikeB)\
**Post date:** [12. August 2020 um 11:30 UTC](https://forum.shopware.com/t/hauptbild-einer-variante-mittels-sql-ermitteln/68917/1 "2020-08-12T11:30:39Z")

</div>

Hallo zusammen.

Ich such gerade verzweifelt eine Möglichkeit das Haupbild einer Variante mittels SQL direkt über die DB zu ermitteln. In der Tabelle s\_articles\_images fehlt leider ein Kennzeichen mit dem ich das Hauptbild identifizieren könnte. Lediglich die Reihenfolge könnte Aufschluss geben aber hier habe ich auch noch keine Möglichkeit gefunden die Einträge mit der kleinsten Reihenfolge zu gruppieren.&nbsp;

Hier mal mein Ansatz einer Mapping-Tabelle:

```
SELECT ai.*, aid.article_detail_id, aid.img, aid.parent_id, m.path
FROM s_articles_img ai 
INNER JOIN s_articles_img aid ON aid.parent_id = ai.id 
INNER JOIN s_media m ON m.id = ai.media_id
GROUP BY aid.parent_id
ORDER BY ai.position ASC

```

Jemand eine Idee? Hocke da schon ne gefühlte Ewigkeit dran.

Gruß Mike

---

<div class="post-metadata">

**Author:** ![Moin](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@Moin](https://forum.shopware.com/u/Moin)\
**Post date:** [14. August 2020 um 12:53 UTC](https://forum.shopware.com/t/hauptbild-einer-variante-mittels-sql-ermitteln/68917/2 "2020-08-14T12:53:22Z")

</div>

Wie lang ist deine Ewigkeit in Stunden, Tagen, Wochen?

---

<div class="post-metadata">

**Author:** ![Moin](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@Moin](https://forum.shopware.com/u/Moin)\
**Post date:** [14. August 2020 um 12:53 UTC](https://forum.shopware.com/t/hauptbild-einer-variante-mittels-sql-ermitteln/68917/3 "2020-08-14T12:53:58Z")

</div>

Wozu brauchst du die Info?

---

<div class="post-metadata">

**Author:** ![\_MikeB](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/_mikeb/32/7856_2.png) [@\_MikeB](https://forum.shopware.com/u/_MikeB)\
**Post date:** [14. August 2020 um 13:23 UTC](https://forum.shopware.com/t/hauptbild-einer-variante-mittels-sql-ermitteln/68917/4 "2020-08-14T13:23:20Z")

</div>

Eine Zeit lang. Ich brauche die Infos um mit den Daten einen anderen Dienst zu bedienen. SQL deshalb damit das auch bei einigen Tausend Produkten sehr performat läuft 😉

---

<div class="post-metadata">

**Author:** ![shyim](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/shyim/32/7681_2.png) [@shyim](https://forum.shopware.com/u/shyim)\
**Post date:** [16. August 2020 um 10:54 UTC](https://forum.shopware.com/t/hauptbild-einer-variante-mittels-sql-ermitteln/68917/5 "2020-08-16T10:54:11Z")

</div>

Schau mal hier&nbsp;[https://github.com/shopware/shopware/blob/5.6/engine/Shopware/Bundle/StoreFrontBundle/Gateway/DBAL/VariantMediaGateway.php](https://github.com/shopware/shopware/blob/5.6/engine/Shopware/Bundle/StoreFrontBundle/Gateway/DBAL/VariantMediaGateway.php)

---

<div class="post-metadata">

**Author:** ![\_MikeB](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/_mikeb/32/7856_2.png) [@\_MikeB](https://forum.shopware.com/u/_MikeB)\
**Post date:** [17. August 2020 um 06:57 UTC](https://forum.shopware.com/t/hauptbild-einer-variante-mittels-sql-ermitteln/68917/6 "2020-08-17T06:57:54Z")

</div>

Hi @Shyim‍

Danke für den Tipp!!&nbsp;Sieht guut aus. Genau das hatte ich gesucht.
