写一个查询更高效,mysql

时间:2013-10-18 13:02:07

标签: mysql sql optimization query-optimization

表:

A,B,Animal,Car

属性:

A:LinkingID, AnimalID, CarID

B:LinkingID, AnimalID, CarID

Animal:AnimalID, Name , Breed

Car:CarID, Weight

我想要的是什么:

选择As和Bs:

A.LinkingID=B.LinkingID

(Animals with AnimalID same as A.AnimalID).Name = (Animals with AnimalID same as B.AnimalID).Name

(Animals with AnimalID same as A.AnimalID).Breed = (Animals with AnimalID same as B.AnimalID).Breed

(Animals with AnimalID same as A.AnimalID).Color = (Animals with AnimalID same as B.AnimalID).Color

(Cars with CarID same as A.CarID).Weight = (Cars with CarID same as B.CarID).Weight

我写的查询:

    select * from A dzo 
     inner join B sar 
        on dzo.LinkingID = sar.LinkingID
     inner join Car c1
        on c1.CarID = dzo.CarID
      inner join Car c2
        on c2.Weight = c1.Weight
        and c2.CarID = sar.CarID
      inner join Animal v1
        on v1.AnimalID = dzo.AnimalID
      inner join Animal v2
        on v2.Name = v1.Name
        and v2.Breed = v1.Breed
        and v2.AnimalID = sar.AnimalID

;

可以写出更有效的查询吗?

0 个答案:

没有答案