0

我有这个运行良好的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表。

4

1 回答 1

0

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

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

于 2013-10-02T11:01:55.820 回答