0

我有三个表:menu、menu_data、languages。第一个用于存储不需要本地化的菜单信息,第二个包含基于语言的本地化数据(如标题),最后一个包含语言 ID。菜单可以由父母拥有,我想在选择子菜单时提取这个父母。我可以使用这样的嵌套 LEFT JOIN 查询来做到这一点:

SELECT la.title, la.permaname, menu.edittime, menu.id, la2.title AS parent_title, pmenu.id AS parent_id 
FROM fm_menu AS menu 
LEFT JOIN fm_menu_data AS la ON ( menu.id = la.targetid and la.languageid = 1 ) 
LEFT JOIN fm_menu AS pmenu ON pmenu.id = menu.parentmenu 
LEFT JOIN fm_menu_data AS la2 ON ( pmenu.id = la2.targetid and la2.languageid = 1 )

但是,我的问题是,有更紧凑、更快或更正确的方法来做这样的事情吗?

4

1 回答 1

1

您的查询看起来不错。

您可以通过从连接条件中删除不必要的括号并删除可选关键字来使其更加紧凑。AS

这是相同的,但用更少的字符表示:

SELECT la.title, la.permaname, menu.edittime, menu.id, la2.title AS parent_title, pmenu.id AS parent_id 
FROM fm_menu AS menu 
LEFT JOIN fm_menu_data la ON menu.id = la.targetid and la.languageid = 1
LEFT JOIN fm_menu pmenu ON pmenu.id = menu.parentmenu 
LEFT JOIN fm_menu_data la2 ON pmenu.id = la2.targetid and la2.languageid = 1
于 2012-12-14T19:07:56.387 回答