2014-09-03 52 views
0

我有一個包含用於移動運營商的流行設備和品牌的數據庫。MySQL在一個簡單的數據庫上加入聲明

我想爲選擇最流行的MODELS for和account +品牌創建一個連接查詢;

SELECT m1.name FROM 
    models as m1 
    JOIN accounts_to_models as am1 
     ON m1.id = am1.FK_model_id 

    JOIN brands as br 
    on br.id = m1.FK_brand_id 

    JOIN accounts as ac 
    on ac.id = am1.FK_account_id 

    WHERE ac.name = "Operator" and b1.name = "HTC" 
    ORDER BY am1.priority desc limit 10 

回答

0

我猜對brands連接是錯誤的,因爲是在accounts的加入。試試這個:

SELECT m1.name 
FROM models m1 JOIN 
    accounts_to_models am1 
    ON m1.id = am1.FK_model_id JOIN 
    brands as b1 
    ON b1.id = m1.FK_brand_id JOIN 
--------------------^ 
    accounts as ac 
    on ac.id = am1.FK_account_id 
-----------^ 
WHERE ac.name = 'Operator' and b1.name = 'HTC' 
ORDER BY am1.priority desc 
limit 10; 
+0

am1中沒有FK_brand_id,但是謝謝......! – user2988649 2014-09-03 17:37:46

+0

@ user2988649。 。 。這是一個錯字。根據您原先在問題中提供的數據模型,它應該是'm1'。 – 2014-09-03 19:48:36

0

你在第三加盟等同ac.nameam1.FK_account_id。所以。我認爲你需要寫:

ac.id = am1.FK_account_id