# Unknown column 'us.countryID' in der erweiterten SQL-Abfrage

**URL:** <https://forum.shopware.com/t/unknown-column-us-countryid-in-der-erweiterten-sql-abfrage/65046>\
**Category:** Programmierung\
**Created:** [27. Februar 2020 um 12:12 UTC](https://forum.shopware.com/t/unknown-column-us-countryid-in-der-erweiterten-sql-abfrage/65046 "2020-02-27T12:12:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rreimche](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@rreimche](https://forum.shopware.com/u/rreimche)\
**Post date:** [27. Februar 2020 um 12:12 UTC](https://forum.shopware.com/t/unknown-column-us-countryid-in-der-erweiterten-sql-abfrage/65046/1 "2020-02-27T12:12:56Z")

</div>

Hallo \*,

ich versuche eigene Bedingungen und Berechnungen für Versandarten einzurichten und habe diefolgenden erweiterte SQL Anfrage.

&nbsp;

```
MAX(a.topseller) as has_topseller,
MAX(at.attr3) as has_comment,
MAX(b.esdarticle) as has_esd,
(
  SELECT
    countryiso
  FROM
    s_core_countries
  WHERE
    id = us.countryID
) AS lieferland,
MIN(
  (
    SELECT
      1
    FROM
      s_articles_categories
    WHERE
      articleID = a.id
      AND categoryID = 7
  )
) AS contains_moebel,
COUNT(
  (
    SELECT
      1
    FROM
      s_articles_categories
    WHERE
      articleID = a.id
      AND categoryID = 146
  )
) AS anzahl_tassenkomplekte,
COUNT(
  (
    SELECT
      1
    FROM
      s_articles_categories
    WHERE
      articleID = a.id
      AND categoryID = 10
  )
) AS anzahl_gutscheine,
COUNT(a.id) as anzahl_produkte

```

Aktuell scheitert das Öffnen des Warenkorbvorschau mit der folgenden Meldung in Log:&nbsp;

```
... SQLSTATE(42S22): Column not found: 1054 Unknown column 'us.countryID' in 'where clause' in ...

```

Laut Doku muss ich auf ‘s\_user\_shipping\_address’ mithihilfe ‘us’ zugreifen können und in der Tabelle steht auch die Spalte ‘countryID’. Von daher bin ich verwirrt, was das bedeutet soll. Könnte sein, dass die Shippingaddresse für einen Benutzer gar nicht ausgefüllt ist, aber wenn das problemhaft wäre,&nbsp;dann würde ich irgendeine andere Fehlermeldung erwarten…

Bitte um Hilfe.

&nbsp;

MfG  
Roman

&nbsp;

---

<div class="post-metadata">

**Author:** ![Haraldio](https://avatars.discourse-cdn.com/v4/letter/h/e79b87/32.png) [@Haraldio](https://forum.shopware.com/u/Haraldio)\
**Post date:** [27. Februar 2020 um 12:23 UTC](https://forum.shopware.com/t/unknown-column-us-countryid-in-der-erweiterten-sql-abfrage/65046/2 "2020-02-27T12:23:52Z")

</div>

Bin jetzt nicht so der Held in SQL - aber hier fehlt für mich die Definition der Tabelle, die mit a.id angesprochen wird, oder nicht? Ist Dein Statement oben vielleicht unvollständig?

---

<div class="post-metadata">

**Author:** ![Haraldio](https://avatars.discourse-cdn.com/v4/letter/h/e79b87/32.png) [@Haraldio](https://forum.shopware.com/u/Haraldio)\
**Post date:** [27. Februar 2020 um 12:27 UTC](https://forum.shopware.com/t/unknown-column-us-countryid-in-der-erweiterten-sql-abfrage/65046/3 "2020-02-27T12:27:05Z")

</div>

… und „us“ ist logischerweise nicht definiert. Ich kenne die Stelle in der Doku nicht, aber vielleicht ist das einfach eine Abkürzung von „s\_user\_shipping\_address.countryID“?

---

<div class="post-metadata">

**Author:** ![rreimche](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@rreimche](https://forum.shopware.com/u/rreimche)\
**Post date:** [27. Februar 2020 um 12:45 UTC](https://forum.shopware.com/t/unknown-column-us-countryid-in-der-erweiterten-sql-abfrage/65046/4 "2020-02-27T12:45:14Z")

</div>

Ja, das wundert mich ebenfalls, dass die “Variablen” nicht definiert sind und auch bin ich keine SQL-Held. Die komplette problemhafte Anfrage sieht so aus:

```
SELECT
  MIN(d.instock >= b.quantity) as instock,
  MIN(d.instock >=(b.quantity + d.stockmin)) as stockmin,
  MIN(a.laststock) as laststock,
  SUM(d.weight * b.quantity) as weight,
  SUM(IF(a.id, b.quantity, 0)) as count_article,
  MAX(b.shippingfree) as shippingfree,
  SUM(
    IF(
      b.modus = 0,
      b.quantity * CAST(b.price as DECIMAL(10, 2)) / b.currencyFactor,
      0
    )
  ) as amount,
  SUM(
    IF(
      b.modus = 0,
      b.quantity * ROUND(CAST(b.price as DECIMAL(10, 2)) /(100 + t.tax) * 100, 2) / b.currencyFactor,
      0
    )
  ) as amount_net,
  SUM(CAST(b.price as DECIMAL(10, 2)) * b.quantity) as amount_display,
  MAX(d.length) as `length`,
  MAX(d.height) as height,
  MAX(d.width) as width,
  u.id as userID,
  MAX(a.topseller) as has_topseller,
  MAX(at.attr3) as has_comment,
  MAX(b.esdarticle) as has_esd,
  (
    SELECT
      countryiso
    FROM
      s_core_countries
    WHERE
      id = us.countryID
  ) AS lieferland,
  MIN(
    (
      SELECT
        1
      FROM
        s_articles_categories
      WHERE
        articleID = a.id
        AND categoryID = 7
    )
  ) AS contains_moebel,
  COUNT(
    (
      SELECT
        1
      FROM
        s_articles_categories
      WHERE
        articleID = a.id
        AND categoryID = 146
    )
  ) AS anzahl_tassenkomplekte,
  COUNT(
    (
      SELECT
        1
      FROM
        s_articles_categories
      WHERE
        articleID = a.id
        AND categoryID = 10
    )
  ) AS anzahl_gutscheine,
  COUNT(a.id) as anzahl_produkte,
  (
    CASE WHEN lieferland = 'DE' THEN 4.95 * (1 + anzahl_tassenkomplekte) WHEN lieferland = 'AT' THEN 7.95 * (1 + anzahl_tassenkomplekte) WHEN anzahl_tassenkomplekte = 0 THEN CASE WHEN lieferland = 'NL' THEN 7.95 WHEN lieferland = 'DK' || lieferland = 'FR' || lieferland = 'IT' || lieferland = 'IE' THEN 9.95 WHEN lieferland = 'ES' || lieferland = 'GB' || lieferland = 'LU' THEN 10.95 WHEN lieferland = 'CH' THEN 19.95 else 999 END else 999 END
  ) as calculation_value_10,
  (
    6 + (
      CASE WHEN lieferland = 'DE' THEN 4.95 * (1 + anzahl_tassenkomplekte) WHEN lieferland = 'AT' THEN 7.95 * (1 + anzahl_tassenkomplekte) WHEN anzahl_tassenkomplekte = 0 THEN CASE WHEN lieferland = 'NL' THEN 7.95 WHEN lieferland = 'DK' || lieferland = 'FR' || lieferland = 'IT' || lieferland = 'IE' THEN 9.95 WHEN lieferland = 'ES' || lieferland = 'GB' || lieferland = 'LU' THEN 10.95 WHEN lieferland = 'CH' THEN 19.95 else 999 END else 999 END
    )
  ) as calculation_value_11,
  ('') as calculation_value_12,
  ('') as calculation_value_13,
  ('') as calculation_value_14,
  ('') as calculation_value_15
FROM
  s_order_basket b
  LEFT JOIN s_articles a ON b.articleID = a.id
  AND b.modus = 0
  AND b.esdarticle = 0
  LEFT JOIN s_user u ON u.id = null
  AND u.active = 1
  LEFT JOIN s_articles_details d ON (d.ordernumber = b.ordernumber)
  AND d.articleID = a.id
  LEFT JOIN s_core_tax t ON t.id = a.taxID
  LEFT JOIN s_articles_attributes at ON at.articledetailsID = d.id
  LEFT JOIN s_user_addresses ub ON ub.user_id = u.id
  AND ub.id = 0
  LEFT JOIN s_user_addresses us ON us.user_id = u.id
  AND us.id = 0
WHERE
  b.sessionID = "blablabla"
GROUP BY
  b.sessionID

```

Laut der Doku sollen die “Variablen” vorhanden sein, das findet man hier in “Vorwort”:&nbsp;[Shopware 5 - Versand- & Zahlungsarten - Individuelle Versandkosten](https://docs.shopware.com/de/shopware-5-de/versand-und-zahlungsarten/individuelle-versandkosten#eigene-berechnungen)

---

<div class="post-metadata">

**Author:** ![sschreier](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/sschreier/32/23296_2.png) [@sschreier](https://forum.shopware.com/u/sschreier)\
**Post date:** [27. Februar 2020 um 12:50 UTC](https://forum.shopware.com/t/unknown-column-us-countryid-in-der-erweiterten-sql-abfrage/65046/5 "2020-02-27T12:50:36Z")

</div>

Hallo,

die Variable hat sich geändert, diese muss nicht mehr „us.countryID“ sondern „us.country\_id“ heißen (das steht ja auch bei „Variablen ab Shopware 5.3.0“ deutlich da).

Grüße

Sebastian

---

<div class="post-metadata">

**Author:** ![rreimche](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@rreimche](https://forum.shopware.com/u/rreimche)\
**Post date:** [27. Februar 2020 um 13:32 UTC](https://forum.shopware.com/t/unknown-column-us-countryid-in-der-erweiterten-sql-abfrage/65046/6 "2020-02-27T13:32:24Z")

</div>

Danke, sschreier. Das hat geholfen.

Jetzt habe ich das folgende:&nbsp;

```
Column not found: 1054 Unknown column 'lieferland' in 'field list'

```

wobei ‚lieferland‘ hier definiert wird:&nbsp;

```
(
    SELECT
      countryiso
    FROM
      s_core_countries
    WHERE
      id = us.countryID
  ) AS lieferland,

```

Das geht, glaube ich, die Grenzen meines SQL-Verständnisses aus. Könnte mir jemand auch hier helfen? Danke im Vorab!

MfG  
Roman

---

<div class="post-metadata">

**Author:** ![sacrofano](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@sacrofano](https://forum.shopware.com/u/sacrofano)\
**Post date:** [27. Februar 2020 um 17:41 UTC](https://forum.shopware.com/t/unknown-column-us-countryid-in-der-erweiterten-sql-abfrage/65046/7 "2020-02-27T17:41:49Z")

</div>

```
MAX((
    SELECT
      countryiso
    FROM
      s_core_countries
    WHERE
      id = us.countryID
  )) AS lieferland,

```

&nbsp;

---

<div class="post-metadata">

**Author:** ![rreimche](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@rreimche](https://forum.shopware.com/u/rreimche)\
**Post date:** [27. Februar 2020 um 22:06 UTC](https://forum.shopware.com/t/unknown-column-us-countryid-in-der-erweiterten-sql-abfrage/65046/8 "2020-02-27T22:06:46Z")

</div>

> [@sacrofano schrieb:](https://forum.shopware.com/profile/25366/sacrofano "sacrofano")
> 
> MAX((  
> SELECT  
> countryiso  
> FROM  
> s\_core\_countries  
> WHERE  
> id = us.countryID  
> )) AS lieferland,

Danke fürs Versoch, aber das hat nicht geändert.
