Web2Py DAL,左连接和运算符优先

时间:2013-12-03 22:29:19

标签: python mysql sql left-join web2py

在我的数据库中,我基本上有3个表:

  1. usergroup(id,name,deleted)
  2. usergroup_presentation(id,groupid,presentationid)
  3. 演示文稿(身份证,姓名)
  4. 我正在尝试运行此DAL查询:

    left_join = db.usergroup_presentation.on((db.usergroup_presentation.group_id==db.usergroup.id)
    &(db.usergroup_presentation.presentation_id==db.presentation.id))
    result = db(db.usergroup.deleted==False).select(
           db.usergroup.id, 
           db.usergroup.name, 
           db.usergroup_presentation.id, 
           left=left_join, 
           orderby=db.usergroup.name)
    

    SQL返回此错误:Unknown column 'presentation.id' in 'on clause'

    生成的SQL看起来像这样:

    SELECT  usergroup.id, usergroup.name, usergroup_presentation.id 
    FROM presentation, usergroup
    LEFT JOIN usergroup_presentation ON ((usergroup_presentation.group_id = usergroup.id) AND (usergroup_presentation.presentation_id = presentation.id)) 
    WHERE (usergroup.deleted = 'F')
    ORDER BY usergroup.name;
    

    我做了一些关于谷歌的研究,我得到了这个: http://mysqljoin.com/joins/joins-in-mysql-5-1054-unknown-column-in-on-clause/

    然后我尝试直接在我的数据库中运行此查询:

    SELECT  usergroup.id, usergroup.name, usergroup_presentation.id 
    FROM (presentation, usergroup)
    LEFT JOIN usergroup_presentation ON ((usergroup_presentation.group_id = usergroup.id) AND (usergroup_presentation.presentation_id = presentation.id)) 
    WHERE (usergroup.deleted = 'F')
    ORDER BY usergroup.name;
    

    FROM表格周围添加括号时确实有效。

    我的问题是如何在不执行基本executesql的情况下使用DAL生成这样的(使用括号)SQL查询?

    更好的是,我希望使用INNER JOINLEFT JOIN获得更清晰的SQL查询。我不知道我的查询是否可行。

1 个答案:

答案 0 :(得分:1)

我相信现在已经在行李箱中修复了。请帮我们检查一下。附:下次打开一张票(https://code.google.com/p/web2py/issues/list),它会很快修好。