php mysql query - 添加更多字段时文档为空

时间:2013-10-02 10:50:49

标签: php mysql sql

我有这个运行正常的mysql查询:

$query = "SELECT
          USER.USER_ID, NAME, SURNAME, EMAIL, STATUS, PHOTO
          FROM FRIENDLIST, USER, USER_PROFILE
          WHERE FRIENDLIST.FRIEND_ID = ANY (
              SELECT FRIEND_ID FROM FRIENDLIST WHERE USER_ID=".$userID."
          )
          AND APPROVED='YES'
          AND USER.USER_ID=FRIENDLIST.FRIEND_ID
          AND USER.USER_ID=USER_PROFILE.USER_ID
          UNION
          SELECT
          USER.USER_ID, NAME, SURNAME, EMAIL, STATUS, PHOTO FROM FRIENDLIST, USER, USER_PROFILE
          WHERE FRIENDLIST.FRIEND_ID = " . $userID . "
          AND APPROVED = 'YES'
          AND USER.USER_ID=FRIENDLIST.USER_ID
          AND USER.USER_ID=USER_PROFILE.USER_ID";

当我在查询中添加更多字段时,我得到“文档为空”错误。

$query = "SELECT
          USER.USER_ID, NAME, SURNAME, EMAIL, STATUS, PHOTO, gender, cposition, rinterest, about
          FROM FRIENDLIST, USER, USER_PROFILE
          WHERE FRIENDLIST.FRIEND_ID = ANY (
              SELECT FRIEND_ID FROM FRIENDLIST WHERE USER_ID=" . $userID . "
          )
          AND APPROVED='YES'
          AND USER.USER_ID=FRIENDLIST.FRIEND_ID
          AND USER.USER_ID=USER_PROFILE.USER_ID
          UNION
          SELECT
          USER.USER_ID, NAME, SURNAME, EMAIL, STATUS, PHOTO
          FROM FRIENDLIST, USER, USER_PROFILE
          WHERE FRIENDLIST.FRIEND_ID = ".$userID."
          AND APPROVED='YES'
          AND USER.USER_ID=FRIENDLIST.USER_ID
          AND USER.USER_ID=USER_PROFILE.USER_ID";

新字段属于USER_PROFILE表。

1 个答案:

答案 0 :(得分:0)

您正在使用UNION,因此SELECT个查询必须具有完全相同的列数。否则,您的查询将失败,并且可能会出现您所看到的错误。此错误消息可能还表示未检查查询结果是否有错误或失败。

如果代码格式正确,这将更容易发现。允许(并建议)在SQL查询中使用输入。