我编写了一个查询,它正在选择有时为NULL的列。
$q = "SELECT g.id AS gameid,g.home_user, g.home_user2, g.away_user, g.away_user2, g.home_score, g.away_score, g.date_confirmed, g.type,
h1.username AS home_username, h2.username AS home_username2, a1.username AS away_username2, a2.username AS away_username2
FROM ".TBL_FOOT_GAMES." g
INNER JOIN ".TBL_USERS." h1 ON h1.id = g.home_user
INNER JOIN ".TBL_USERS." h2 ON h2.id = g.home_user2
INNER JOIN ".TBL_USERS." a1 ON a1.id = g.away_user
INNER JOIN ".TBL_USERS." a2 ON a2.id = g.away_user2
WHERE g.status = '$status'
ORDER BY g.date_confirmed DESC LIMIT 25";
列
home_user2和away_user2
有时可以为NULL。显然,这似乎阻止查询显示任何返回的数据。 当我从查询中删除这些列时,我得到了一个返回。 包括它们不会引发错误,只是简单就是不选择任何行。
我怎样才能解决这个问题?
答案 0 :(得分:2)
您需要使用left join,而不是这些列上的内部联接。
$q = "SELECT g.id AS gameid,g.home_user, g.home_user2, g.away_user, g.away_user2, g.home_score, g.away_score, g.date_confirmed, g.type,
h1.username AS home_username, h2.username AS home_username2, a1.username AS away_username2, a2.username AS away_username2
FROM ".TBL_FOOT_GAMES." g
INNER JOIN ".TBL_USERS." h1 ON h1.id = g.home_user
LEFT JOIN ".TBL_USERS." h2 ON h2.id = g.home_user2
INNER JOIN ".TBL_USERS." a1 ON a1.id = g.away_user
LEFT JOIN ".TBL_USERS." a2 ON a2.id = g.away_user2
WHERE g.status = '$status'
ORDER BY g.date_confirmed DESC LIMIT 25";