0

我正在尝试从我的一个表中选择用户数据行。我只想要数组中具有primary_key(user_id)的行。我怎样才能做到这一点?

创建数组的查询:

$suggestions_query= "
SELECT * 
  FROM relations 
 WHERE user1= '$user_id' 
   AND status = 1;
";
$suggestions_array= mysqli_query($connect, $suggestions_query);

查询获取用户数据:

$search_query= "
SELECT * 
  FROM users 
 WHERE fname LIKE '%".$fname."%' 
   AND lname LIKE '%".$lname."%'" 
   AND IN ARRAY <!-- Code in Question

$search_result= mysqli_query($connect, $search_query)
4

4 回答 4

1

如果我正确理解了这个问题,听起来您想使用内部联接。你可以在这里阅读它们.. http://www.w3schools.com/sql/sql_join.asp

语法类似于..

"SELECT * FROM relations "
. "INNER JOIN users ON relations.user_id=users.user_id "
. "WHERE fname LIKE '%".$fname."%' AND lname LIKE '%".$lname."%' "
. "AND user1= '".$user_id."' AND status= 1"

这两个表必须具有 user_id 的并行值,即,您将 user_id 视为外键。

希望这可以帮助

于 2013-07-09T15:25:12.677 回答
1

尝试在 1 个查询中完成所有操作

SELECT * FROM users 
WHERE fname LIKE '%".$fname."%' 
AND lname LIKE '%".$lname."%' 
AND user_id IN (
               SELECT user_id FROM relations
               WHERE user1= '".$user_id."' 
               AND status= 1)
于 2013-07-09T15:17:04.610 回答
0

您可以使用子查询。

您可以将这两个查询组合成如下内容:

$search_query= "SELECT * FROM users WHERE fname LIKE '%".$fname."%' AND lname LIKE 
'%".$lname."%'" AND id in (SELECT id FROM relations where user1= '".$user_id."' AND status= 1)"
于 2013-07-09T15:16:25.520 回答
0
SELECT * FROM users WHERE fname LIKE '%".$fname."%' AND lname LIKE 
'%".$lname."%'" AND user_id IN (SELECT user_id FROM relations WHERE user1= '".$user_id."' AND STATUS= 1)
于 2013-07-09T15:17:16.147 回答