选择两个表中的修订版并使用内部联接表转换

时间:2014-04-07 02:19:31

标签: php mysql sql join

我有4张桌子:
评分 大学 学校 翻译
获得大学或学校的评估等级,翻译包含翻译信息。

等级

user | business_id | rate
1    | 1           | text my rate

大学和学校

business_id | translate_id
1           | 1

翻译

translate_id | text   | language
1            | street | en
2            | strada | it

我需要获取用户ns大学和学校表的所有评论并使用翻译包 我正在使用下面的查询,但我怀疑它是最好的方式。
我发现它有点复杂,并且想知道是否还有其他更简单的替代强文本。


我的查询

select
    A.* , innerjoin.name , innerjoin.address , innerjoin.type
from rating as A
inner join (
    select t.address , B.id , B.name , "university" as type from university as B
        inner join translate as t on t.id = B.address where t.language = "en"

    union all

    select t.address , B.id , B.name , "school" as type from school as B
        inner join translate as t on t.id = B.address where t.language = "en"
) innerjoin
on innerjoin.id = A.id
where A.student = "John"



我的输出 - EN

0 => Array
     rate => #2 my rate
     name => school-1
     address => street
     type => school
1 => Array
     rate => #3 my rate
     name => university-1
     address => mall
     type => university
2 => Array
     rate => #1 my rate
     name => school-2
     address => avenue
     type => school



我的输出 - IT

0 => Array
     rate => #2 my rate
     name => school-1
     address => strada
     type => school
1 => Array
     rate => #3 my rate
     name => university-1
     address => centro commerciale
     type => university
2 => Array
     rate => #1 my rate
     name => school-2
     address => viale
     type => school

0 个答案:

没有答案