Php 其中包含多个表的条件

Php 其中包含多个表的条件,php,mysql,Php,Mysql,我正在删除与所有四个表中的用户名匹配的表内容..即..如果我从第一个表中删除用户名“user1”的一条记录..它应该删除用户名为“AAA”的所有表中的记录 我用的桌子是 table 1 : p_user_profile p_id username ---- -------- 1 AAA 表2:p_设备id d_id username ---- -------- 1 AAA 表3:p_临时通

我正在删除与所有四个表中的用户名匹配的表内容..即..如果我从第一个表中删除用户名“user1”的一条记录..它应该删除用户名为“AAA”的所有表中的记录

我用的桌子是

table 1 : p_user_profile

p_id        username  
----        --------
1           AAA
表2:p_设备id

d_id        username 
----        --------
1           AAA
表3:p_临时通知

p_id        username   type
----        --------   -----
1           AAA          i
p_id        username   type
----        --------   -----
1           AAA          i
表4:p_发送的通知

p_id        username   type
----        --------   -----
1           AAA          i
p_id        username   type
----        --------   -----
1           AAA          i
在这里,我想从所有表中删除与用户名匹配的记录,并从3和4个表中删除与of type=“i”匹配的记录

下面是我使用的代码:

$p_id = intval($_REQUEST['p_id']);
include 'db/connection.php';
$sql= "DELETE p_user_profile, p_device_id, p_sent_notification FROM p_user_profile LEFT JOIN p_device_id ON p_device_id.username = p_user_profile.username
                                                                                   LEFT JOIN p_sent_notification ON p_device_id.username = p_sent_notification.username WHERE p_id = $p_id AND p_sent_notification.type = "i" ";
在ablove查询中,如果我使用一个WHERE条件执行,它将成功执行..但它将删除所有记录的类型..但我只想删除类型为=“i”的记录


Plz解析帮助..谢谢

您正在查询中使用p\u sent\u notification,而表名为sent\u notification
intval
不能替代。请尝试将查询更改为使用单引号,像这样:
p\u sent\u notification.type='i'
@tadman对于数字来说就足够了。。。字符串将转换为0。如果不使用准备好的语句,有时使用intval比使用_real_escape_string更好。引用yes,我认为在where中定义要使用哪个表的p_id也不错,因为在3个表中都有p_id。@Mr.Smith除非你有很好的理由,否则应该使用准备好的语句。如果你在做正确的事情时严格遵守纪律,那么就很难让他们犯错。依靠
intval
等技巧进行转义,将空字符串转换为值,并不总是一个好的计划。我错误地将表名指定为sent_notification。它应该是p_sent_notification。在查询中,我正确地指定了。但它不起作用