我正在尝试从我的一个表中选择用户数据行。我只想要在数组中有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)
答案 0 :(得分: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)
答案 1 :(得分: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视为外键。
希望这有帮助
答案 2 :(得分: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)"
答案 3 :(得分: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)