我有3个表,我查询了第一个表(用户)以获得此结果:
$String_users='19,20,21,22,25,26,27,28,29,30,31,32,33,34';
我希望过滤其他两个表朋友和freinds_request并删除数字中的ID,以便我稍后再做一些事情,但是由于我的理解不好,它会返回此错误:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\cebs\include\functions.php on line 825
然后我尝试从sql命令运行查询 - phpmyadmin来查看错误:
SELECT id FROM users WHERE id NOT IN
(SELECT user_one,user_two FROM friends WHERE (user_one='18' AND user_two IN('19,20,21,22,25,26,27,28,29,30,31,32,33,34'))
OR (user_one IN('19,20,21,22,25,26,27,28,29,30,31,32,33,34') AND user_two='18'))
AND id NOT IN( SELECT to_user,from_user FROM friend_reqest WHERE (to_user='18' AND from_user IN('19,20,21,22,25,26,27,28,29,30,31,32,33,34') OR (to_user IN('19,20,21,22,25,26,27,28,29,30,31,32,33,34') AND from_user='18'))
)
)
但是MYSQL说:
#1241 - Operand should contain 1 column(s)
下面是我的PHP代码:
function somepeopleyoumayknow(){
global $dbc_conn, $IsLoggIn,$table_name,$friend_request_table,$friends_table ;
$cu_school = getuser($IsLoggIn,'cell_group');
//assuming $IsLoggIn is equal to 18...
$peopleids= mysqli_query($dbc_conn,"SELECT id FROM $table_name WHERE id !='$IsLoggIn'");
$sql_num_rows = mysqli_num_rows($peopleids);
if($sql_num_rows > 0){
while($run_peopleids= mysqli_fetch_array($peopleids)){
$users_ids[] = $run_peopleids['id'];
}
$string_users = implode(',',$users_ids);
$sql = "SELECT id FROM $table_name WHERE id NOT IN
(SELECT user_one,user_two FROM $friends_table WHERE (user_one='$IsLoggIn' AND user_two IN($string_users))
OR (user_one IN($string_users) AND user_two='$IsLoggIn'))
AND id NOT IN( SELECT to_user,from_user FROM $friend_request_table WHERE (to_user='$IsLoggIn' AND from_user IN($string_users) OR (to_user IN($string_users) AND from_user='$IsLoggIn'))
)";
$filter_id_query = mysqli_query($dbc_conn,$sql);
$fnrows = mysqli_num_rows($filter_id_query);
if($fnrows > 0){
while($run_fiq=mysqli_fetch_array($filter_id_query)){
$uid[] = $run_fiq['id'];
}
echo $filtered_id_users = implode(',',$uid);
}
}
}
以下是存储的表格和数据:
如何解决此问题?谢谢。
答案 0 :(得分:0)
<强>已更新强>: 好的,那么你的sql语句应该是这样的:
SELECT * FROM (
SELECT user_one as id
FROM friends
WHERE user_one != :loggedInId
UNION
SELECT user_two as id
FROM friends
WHERE user_two != :loggedInId
)
WHERE id NOT IN (
SELECT from_user as id
FROM friend_request
WHERE to_user = :loggedInId
UNION
SELECT to_user as id
FROM friend_request
WHERE from_user = :loggedInId
)
P.S。当然,请使用预备语句。我建议使用PDO。如果您没有可能,请使用mysqli::prepare方法。
它会使您的代码看起来像这样(只需将:loggedInId
更改为?
):
if ($stmt = $mysqli->prepare($sql)) {
$stmt->bind_param('i', $loggedInId);
$stmt->execute();
$stmt->fetch();
$stmt->close();
}
答案 1 :(得分:0)
有一个问题,比如在()中删除single quote
,否则会考虑单个字符串:
(SELECT user_one,user_two FROM friends WHERE (user_one='18' AND user_two IN('19,20,21,22,25,26,27,28,29,30,31,32,33,34')) OR (user_one IN('19,20,21,22,25,26,27,28,29,30,31,32,33,34') AND user_two='18')) AND id NOT IN( SELECT to_user,from_user FROM friend_reqest WHERE (to_user='18' AND from_user IN('19,20,21,22,25,26,27,28,29,30,31,32,33,34') OR (to_user IN('19,20,21,22,25,26,27,28,29,30,31,32,33,34') AND from_user='18')) ) )
请更新如下:
(SELECT user_one,user_two FROM friends WHERE (user_one='18' AND user_two IN(19,20,21,22,25,26,27,28,29,30,31,32,33,34)) OR (user_one IN(19,20,21,22,25,26,27,28,29,30,31,32,33,34) AND user_two='18')) AND id NOT IN( SELECT to_user,from_user FROM friend_reqest WHERE (to_user='18' AND from_user IN(19,20,21,22,25,26,27,28,29,30,31,32,33,34) OR (to_user IN(19,20,21,22,25,26,27,28,29,30,31,32,33,34) AND from_user='18')) ) )
答案 2 :(得分:0)
您的查询在逻辑上不正确,当您执行where子句时,子选择如下:
var re = /@(\w+)/;
var arr = ['@one', 'two', '@three'];
arr.forEach(function(str) {
if (str.match(re))
document.writeln(str.match(re)[1] + '<br>');
});
您应该只选择1个操作数/列,即子查询中的columnX。 见下面的粗体:
WHERE <columnX> NOT IN (SELECT <columnX from table x)
答案 3 :(得分:0)
读到Dimitiri发布的内容时,我正在思考并重新编写下面的SQL语句是我的回答:
$sql = "SELECT id FROM $table_name WHERE id NOT IN
(SELECT user_one FROM $ friends_table WHERE(user_one ='$ IsLoggIn'AND user_two IN($ string_users)) 要么 (user_one IN($ string_users)AND user_two ='$ IsLoggIn'))AND id NOT IN(SELECT user_two FROM $ friends_table WHERE(user_one ='$ IsLoggIn'AND user_two IN($ string_users))OR (user_one IN($ string_users)AND user_two ='$ IsLoggIn'))
AND id NOT IN (SELECT from_user FROM $ friend_request_table WHERE(from_user ='$ IsLoggIn'AND to_user IN($ string_users))OR(from_user IN($ string_users)AND to_user ='$ IsLoggIn'))
AND id NOT IN (SELECT to_user FROM $ friend_request_table WHERE(from_user ='$ IsLoggIn'AND to_user IN($ string_users))OR(from_user IN($ string_users)AND to_user ='$ IsLoggIn'))“;
这会过滤并删除两个表中的所有ID。 以前在我的朋友表中我有5个ID代表朋友,在上面运行此查询时,这些ID被删除:
下面是一张证明它的照片:
$ String_users = '19,20,21,22,25,26,27,28,29,30,31,32,33,34' ;
$ filtered_id_users = 19,25,27,28,29,31,32,34
说明:
这意味着我已经是ids的朋友:21,22,26,30(朋友表)
和
我有一个朋友请求,意味着id 33(friend_requst_table)