Php 如果mysql依赖于数组数据,如何使用NOT IN子句
上表显示了问题及其IDPhp 如果mysql依赖于数组数据,如何使用NOT IN子句,php,mysql,Php,Mysql,上表显示了问题及其ID questions q_id|question 1|What is the population of earth 2|Who is tallest person in the world 3|what makes people to cheat 这显示了用户回答的(q_id)问题 我正在努力检索用户没有回答的问题。 这是我的问题 user user_id|q_id 2013 |1 2013 |2 1785 |3 通过循环
questions
q_id|question
1|What is the population of earth
2|Who is tallest person in the world
3|what makes people to cheat
这显示了用户回答的(q_id)问题
我正在努力检索用户没有回答的问题。
这是我的问题
user
user_id|q_id
2013 |1
2013 |2
1785 |3
通过循环获取数据并将其分配给名为$questions的数组
我的目标是让没有人回答的问题这就是我所做的
$sql =sprintf("SELECT * FROM users WHERE user_id = '$user' ");
这不起作用。
左连接是一个很好的解决方案
$query =sprintf("SELECT * FROM nyuku_questions WHERE q_id NOT IN($quesition ) order by rand() limit 1")
它将用户表中的所有行与问题表中的相应行相匹配。如果没有匹配项,它将为所有问题值填写null
。我们只需在where
子句中对其进行筛选
如果要在
中使用非,则如下所示:
select user.*
from user
left join questions
on user.q_id = questions.q_id
where questions.q_id is null;
您还没有找到解决方案:)
当然,我们假设$userid在作为参数发送之前已经过检查/验证我想我在寻找与您的答案相反的答案,因为这个查询(左连接)它实际上是在检索那个人已经回答过的数据,我在寻找没有回答过的问题。在你给我关于左连接的提示后,我尝试使用完整的外部连接,但我没有工作also@ReginwaldtLed哎呀。我输入了第一个问题(老实说,我犯了一个错误),请再试一次。我使用了
=null
而不是is null
——这是两个完全不同的结果
select *
from questions q
where q.q_id not in (
select q_id from user
where user_id = "USER ID YOU ARE LOOKING FOR"
)
SELECT * FROM questions WHERE q_id NOT IN(SELECT q_id FROM user WHERE user_id = '". $userid ."') order by rand() LIMIT 1;