2016-08-09 4 views
0

Im eine Kombination von ids aus einer Tabelle wie diese zu erhalten versuchen:MYSQL LEFTJOIN mehr ids selber Tisch

Tabelle Aktivierung:

user_id product_id reseller_id range_id Name 
------------------------------------------------- 
1    1   5   2  Oscar 
1    1   5   3  Luis 
2    1   5   4  Julian 

Tabelle Preise (compType_id = reseller_id):

product_id  compType_id  price range_id 
------------------------------------------------ 
1     5   38.60  2 
1     5   48.60  3 
1     5   58.60  4 

Tabelle Benutzer:

user_id name 
---------------- 
    1  lloyd 
    2  Mark 

Ich möchte den Aktivierungsnamen und den Preis der Preise basierend auf der Benutzer-ID auswählen. Wie kann ich das tun?

habe ich versucht, so etwas wie dieses:

SELECT a.name 
(SELECT price FROM prices WHERE product_id = 1 AND range_id = 
AND compType_id = 5) AS price 
FROM activation AS a 
LEFT JOIN users AS u ON u.user_id = a.u_id 
WHERE u.user_id = 1 

der Preis Spalten in jeder Zeile den gleichen Wert haben.

name price 
Oscar 21.30 
Luis 21.30 

Wie kann ich es ändern, um den korrekten Preis anzuzeigen?

Antwort

0

Ich löste es mit etwas wie folgt aus:

SELECT a.company, a.name, a.email, a.phone, a.ruc, a.code, a.active, 
a.numlic, (SELECT p.price FROM prices AS p WHERE p.product_id = 
a.product_id AND p.range_id = a.range_id AND p.compType_id = c.type_id) 
AS price 
FROM activation AS a 
LEFT JOIN users AS u ON u.user_id = a.u_id 
LEFT JOIN companies AS c ON a.reseller_id = c.id 
WHERE u.user_id = 1