0

我正在尝试使用下面的代码从我的数据库中输出所有相关值。由于某种原因,某些信息没有输出,我不知道为什么。

我猜我的查询有问题?

$result = mysql_query(
        "SELECT 

        tbl_status.id as statID, 
        tbl_status.from_user as statFROM, 
        tbl_status.status as statSTATUS, 
        tbl_status.deleted as statDEL, 
        tbl_status.date as statDATE,

        tbl_users.id as usrID, 
        tbl_users.name as usrNAME,

        tbl_photos.profile as photosPROFILE,
        tbl_photos.photo_link as photoLINK,
        tbl_photos.default_photo as photoDEFAULT 

        FROM tbl_status 
        INNER JOIN tbl_users ON tbl_status.from_user = tbl_users.id

        INNER JOIN tbl_photos ON tbl_photos.profile = tbl_users.id 
        WHERE tbl_status.deleted = '0' AND tbl_photos.default_photo = '1'
        ORDER BY tbl_status.date desc;

        ");
4

1 回答 1

0

Based on your query, here are four possible reasons you're not seeing all statuses:

  1. You're using an inner join to tbl_photos, so any user that doesn't have a photo will be excluded.
  2. You're using an inner join to tbl_users, so any status that doesn't relate to at least one user (and the user must have a photo - see #1) will be excluded.
  3. You're filtering on tbl_photos.default_photo = '1', so any photo with tbl_photos.default_photo <> '1' will be excluded. Because of the inner joins, this will also exclude the associated user and possibly exclude the associated status.
  4. You're filtering on tbl_status.deleted = '0', so any status with delete <> '0' will be excluded. Obvious, but worth mentioning.

Try this: change INNER JOIN tbl_users to LEFT JOIN tbl_users, and change INNER JOIN tbl_photos to LEFT JOIN tbl_photos. If that doesn't give you the results you expect, please post sample data and expected results.

于 2013-08-13T20:29:37.757 回答