请参考下图中的2个表格:
代码:
$query41 = "SELECT p.message FROM friendship f
JOIN messageslive p
ON p.username = f.frenusername OR p.username = f.username
WHERE f.username = '{$username1}' OR f.frenusername = '{$username1}'
ORDER BY
p.id DESC
LIMIT 16";
$result41 = mysql_query($query41,$connection) or die (mysql_error());
confirm_query($result41);
while($msginfo = mysql_fetch_array($result41)){
$msg = $msginfo['message'];
echo $msg . "<br/>";
}
输出:
live
sss
ssa
ddd
asa
(dance2)
asaaa
ssa
(bluek2)
(bluek2)
(bluek2)
(bluek2)
(bluek2)
(bluek2)
(bluek2)
(bluek2)
问题:为什么输出显示重复(bluek2)
?如何修复bug?我想显示由zac1987和zac1987的朋友发布的最新16条帖子。预期输出应与表messagelive中的记录相同,例如:
live
sss
ssa
ddd
asa
(dance2)
asaaa
ssa
(bluek2)
jjj
vv
(comp2)
(sad2)
(dance7)
答案 0 :(得分:0)
你甚至需要加入friendship
表吗?
SELECT p.message
FROM messageslive p
WHERE p.username = '{$username1}'
ORDER BY p.id DESC
LIMIT 16
如果你只是想建立友谊的存在,也许:
SELECT p.message
FROM messageslive p
WHERE p.username = '{$username1}'
AND EXISTS(SELECT 1
FROM friendship f
WHERE f.username = p.username
OR f.frenusername = p.username)
ORDER BY p.id DESC
LIMIT 16
答案 1 :(得分:0)
将您的查询更改为此,它应该有效。
$query41 = "SELECT p.message FROM friendship f
JOIN messageslive p
ON p.username = f.frenusername OR p.username = f.username
WHERE f.frenusername = '{$username1}'
ORDER BY
p.id DESC
LIMIT 0, 16";