2017-09-17 2 views
0

Wie ich Spalte für die gleiche Tabelle mit dieser Abfrage aktualisieren können:MySQL Update wählen Sie aus der gleichen Tabelle

update products 
set (related_product_id) = 
(
select GROUP_CONCAT(id) from products 
INNER JOIN products_cross on products_cross.product_upc = products.upc 
WHERE products_cross.related_product_upc = 2631695 
) 

PRODUCTS Tabelle vor:

id | upc | related_product_id | 
3721 | 2631695 |      | 
4566 | 37262 |      | 
3723 | 173615 |      | 
3724 | 216571 |      | 

PRODUCTS Tabelle nach:

id | upc | related_product_id | 
3721 | 2631695 | 4566,3723,3724  | 
4566 | 37262 |      | 
3723 | 173615 |      | 
3724 | 216571 |      | 

PRODUKTE Kreuztabelle:

product_upc | related_product_upc | 
37262  | 2631695    | 
173615  | 2631695    | 
216571  | 2631695    |   
+0

Was Sie versuchen zu aktualisieren.Auf welcher Basis. Bitte erläutern Sie – Arora20

+0

Geben Sie Ihre Tabellendefinitionen an, um Ihr Schema besser zu verstehen –

+0

Ich möchte Produkt-IDs auf Basis einer anderen Tabelle implodieren (products_cross). Nein, die aktuelle Abfrage funktioniert nicht: "Sie können die Zieltabelle 'lc_products' nicht für die Aktualisierung in der FROM-Klausel oder # 1064 angeben. - Sie haben einen Fehler in Ihrer SQL-Syntax. Überprüfen Sie das Handbuch, das Ihrer MariaDB-Serverversion entspricht für die richtige Syntax zu verwenden '(related_product_id) = ( wählen Sie GROUP_CONCAT (ID) von Produkten INNER JOIN' bei Zeile 2 – JoeDoe

Antwort

0

könnte so etwas wie unten

update products p1 
join (
    select c.related_product_upc ,GROUP_CONCAT(product_upc) related_products 
    from products p 
    join products_cross c on c.related_product_upc = p.upc 
    where c.related_product_upc = 2631695 
    group by c.related_product_upc 
) t on(t.related_product_upc = p1.upc) 
set p1.related_product_id = t.related_products; 

DEMO

Hinweis: als comma separated values ​​verwandte Produkte zu speichern ist ein schlechtes Design stattdessen können Sie eine neue Tabelle erstellen und Ihre Produkte beziehen, so dass jede Beziehung wird als einzelne Zeile gespeichert

related_products 

product_id related_product_id 
3721  4566 
3721  3723 
3721  3724 
Verwandte Themen