MySQL中VIEW的SELECT语句没有返回所有想要的行

时间:2010-06-18 20:01:40

标签: sql mysql drupal integration sql-view

我的MySQL查询需要帮助:

CREATE OR REPLACE 
    DEFINER = CURRENT_USER
    VIEW users_phpscheduleit
AS
    SELECT 
            u.uid AS memberid,
            pass AS password, 
            mail AS email,
            pv1.value AS fname,
            pv2.value AS lname,
            pv3.value AS phone,
            e_add, e_mod, e_del, e_html, e_app, 
            is_admin, lang, timezone_scheduleit, 
            institution, logon_name
        FROM 
            users u, profile_values pv1, profile_values pv2, profile_values pv3
        WHERE
            u.uid = pv1.uid AND u.uid = pv2.uid AND u.uid = pv3.uid
            AND 
            pv1.fid = 26
            AND
            pv2.fid = 27
            AND 
            pv3.fid = 8;

这不能包含list of things,以便在我与其他PHP系统集成的PHP应用程序中的INSERT或UPDATE查询中使用视图表保持可编辑状态。我的用户表是442长,我只得到1/2。

问题:
我无法获取users表中的所有行,因为它们显然并非都有{27}或8的profile_values.fid

问:我如何调整声明仍然包括那些但不会破坏the rules以启用可编辑性的声明?

ps.for好奇我试图通过Drupal DB验证phpScheduleIt用户

1 个答案:

答案 0 :(得分:1)

  

我无法获取users表中的所有行,因为它们显然并非都具有26,27或8的profile_values.fid。

这是因为你的查询使用INNER JOIN,当你需要使用OUTER(IE LEFT / RIGHT)JOIN时,如果你想要有可能没有支持记录的数据。您的查询采用ANSI-89语法,该语法不能始终如一地实现OUTER JOIN语法。这是使用ANSI-92语法重写的查询:

   SELECT u.uid AS memberid,
          pass AS password, 
          mail AS email,
          pv1.value AS fname,
          pv2.value AS lname,
          pv3.value AS phone,
          e_add, e_mod, e_del, e_html, e_app, 
          is_admin, lang, timezone_scheduleit, 
          institution, logon_name
     FROM USERS u
LEFT JOIN PROFILE_VALUES pv1 ON pv1.uid = u.uid
                            AND pv1.fid = 26
LEFT JOIN PROFILE_VALUES pv2 ON pv2.uid = u.uid
                            AND pv2.fid = 27
LEFT JOIN PROFILE_VALUES pv3 ON pv3.uid = u.uid
                            AND pv3.fid = 8