Php mysql中如何根据另一个表的结果从一个表中获取内容
我有两个mysql表,其中一个叫做请愿Php mysql中如何根据另一个表的结果从一个表中获取内容,php,mysql,sql,Php,Mysql,Sql,我有两个mysql表,其中一个叫做请愿 {"success":1,"petitions":[{"id":"6","name":"should he go","timestamp":"2013-10-26 03:02:44"},{"id":"3","name":"Olara Otunu should get married","timestamp":"2013-10-24 14:33:53"},{"id":"4","name":"Teachers deserve 30 not 20 salary
{"success":1,"petitions":[{"id":"6","name":"should he go","timestamp":"2013-10-26 03:02:44"},{"id":"3","name":"Olara Otunu should get married","timestamp":"2013-10-24 14:33:53"},{"id":"4","name":"Teachers deserve 30 not 20 salary rise","timestamp":"2013-10-24 14:33:53"},{"id":"5","name":"Prostitution should be banned","timestamp":"2013-10-24 14:33:53"},{"id":"1","name":"Has Jennifer Musisi done great work for Kampala","timestamp":"2013-10-24 14:32:58"},{"id":"2","name":"Do lecturers deserve 100% salary increase","timestamp":"2013-10-24 14:32:58"}]}
另一个表称为请愿响应
{"success":1,"petition_response":[{"id":"2","petitionID":"2","yes":"0","no":"1","memberID":"14","timestamp":"2013-11-02 08:36:20"},{"id":"1","petitionID":"1","yes":"1","no":"0","memberID":"14","timestamp":"2013-11-01 21:26:02"}]}
我需要在请愿书回复中选择没有回复的*请愿书。但是,如果他们在请愿书响应表中有任何响应,则memberID不应等于14
我尝试了下面的代码,但它不起作用
$result = mysql_query("select * from petition_response where memberID='14' order by timestamp DESC", $db->connect());
while($row = mysql_fetch_assoc($result)){
$id = $row['id'];
$result2 = mysql_query("select * from petitions where id != '$id' order by timestamp DESC", $db->connect());
}
return $result2;
您希望实现反连接,MySQL中有三种可能: 不在以下位置使用: 挑选* 请愿书 我不在哪里 选择申请ID 从请愿书到答复 其中memberID=14 使用不存在: 挑选* 来自请愿书p 不存在的地方 挑选* 来自请愿书的回复 其中r.id=p.id r.memberID=14 限制1 使用外部联接: 选择p* 来自请愿书p 左外连接请求\u响应r 在r.id=p.id和r.memberID=14上 其中r.id为空 在电视上看到他们 根据: 总结 MySQL可以优化所有三种方法,以执行某种嵌套循环反连接 它将从t_左侧获取每个值,并在t_right.value上的索引中查找。在索引命中或未命中的情况下,相应的谓词将立即分别返回FALSE或TRUE,并且将立即决定是否从t_左侧返回该行,而不检查t_右侧的其他行 然而,这三种方法生成三种不同的计划,由三段不同的代码执行。执行EXISTS谓词的代码比执行index_子查询和左连接优化为使用Not EXISTS方法的代码的效率低约30% 这就是为什么在MySQL中搜索缺失值的最佳方法是使用LEFT JOIN/is NULL或NOT in而不是NOT EXISTS
请注意,此方法不推荐使用。使用mysqli和/或PDO。看看注射。然后看看联接。它不起作用,不足以帮助您。你得到了什么结果/错误?我试过了,但仍然没有得到正确的结果好吧,但是你现在得到了什么,你期望得到什么?{成功:1,请愿书:[{id:6,姓名:他应该去吗,时间戳:2013-10-26 03:02:44},{id:3,姓名:奥拉拉·奥图努应该结婚,时间戳:2013-10-24 14:33:53},{id:4,姓名:教师应该加薪30而不是20,时间戳:2013-10-24 14:33:53},{id:5,姓名:卖淫应该被禁止,时间戳:2013-10-24 14:33:53},{id:2,姓名:讲师应该加薪100%,时间戳:2013-10-24 14:32:58}]
$query = "
SELECT *
FROM petition_response pr
LEFT
JOIN petitions p
ON p.id = pr.id
WHERE pr.memberID = 14
AND p.id IS NULL
ORDER
BY pr.timestamp DESC;
";