我正在做一个学校的项目,其中包括用php做一个类似facebook的应用程序,我遇到了一个棘手的sql请求问题。
我的数据库配置如下:
我想找到我朋友名单上没有的朋友来推荐他们,
我的sql请求让我的朋友列表正常工作,所以我做的是,我循环我的所有朋友,我选择他们的朋友,但在这里,我不知道如何选择他们,而不选择朋友,已经是我的朋友
下面是我如何循环通过每个朋友:(我不认为这是做这件事的好方法)
<?php
function recommendation($id)
{
global $pdo;
$sql = "SELECT *
FROM user u
LEFT JOIN friends f on u.id = f.iduser
WHERE isvalidate = 1
AND ((f.iduser = ? AND u.id != ?) OR ((f.idfriend = ? AND u.id != ?)))
GROUP BY u.id";
$q = $pdo->prepare($sql);
$q->execute(array($id, $id, $id, $id));
echo "<p>Vous connaisser peut etre:</p>
<ul id ='recommendation'>";
$amiscommun = array();
while ($line = $q->fetch()) {
//recupération de la liste des amis
//Pour chaque amis recupération de leur liste d'amis egalement
$sql = "SELECT *
FROM user u
LEFT JOIN friends f on u.id = f.iduser
WHERE isvalidate = 1
AND (((f.iduser = ? AND u.id != ?) OR ((f.idfriend = ? AND u.id != ?)))
AND ((f.iduser = ? AND u.id != ?) OR ((f.idfriend = ? AND u.id != ?))))
AND (f.iduser != (SELECT * FROM))
GROUP BY u.id";
$q2 = $pdo->prepare($sql);
$q2->execute(array($line['id'], $line['id'], $line['id'], $line['id'], $line['id'], $id, $line['id'], $id));
$amis = array();
while ($line2 = $q2->fetch()) {
echo "<li>" . $line2['login'] . " est un amis de " . $line['login'] . "</li>";
$amis[$line['login']][] = array($line2['login']);
$amiscommunnbr[$line2['login']][] = $line['login'];
//Stockage a chaque iteration de boucle des amis commun dans un tableau
}
}
$newarray = $amiscommunnbr;
if (!empty($newarray)) {
foreach ($newarray as $key => $value) {
$nbr = count($value);
echo "<li><a href='index.php?action=bio?" . $key . "'>" . $key . "</a>- " . $nbr . " Amis en commun(";
foreach ($value as $key2 => $value2) {
echo "<a href='index.php?action=bio?" . $value2 . "'>" . $value2 . "</a>";
}
echo ")</li>";
}
}
echo "</ul>";
}
1条答案
按热度按时间yc0p9oo01#
“select*from friends where iduser in(select group_concat(idfriend)form user u join friends where f.iduser=u.id and u.id=$uid)和id not in(select group_concat(id)from friends where iduser=$id)”;
:我想这对你有帮助。如果您对此有任何问题,请告诉我我已经更新了查询。