关于简单数据库的MySQL Join Statement

时间:2014-09-03 17:21:20

标签: mysql join mysql-workbench

我有一个包含移动运营商流行设备和品牌的数据库。

我想制作一个联接查询,以选择最受欢迎的MODELS和帐户+品牌;

   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

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

您在第三次加入中将 ac.name am1.FK_account_id 等同起来。所以。我想你需要写:

ac.id = am1.FK_account_id