php - Delete from inner join mysql query - two tables multiple conditions -
i have 2 mysql tables:
table allproducts accountid, productowner, productnumber 100001, tom, abc1 100001, tom, abc2 100001, greg, abc3 100002, charlie, abc2 table productdata accountid, productnumber, productdesc 100001, abc1, deschere 100001, abc2, deschere 100001, abc3, deschere 100002, abc2, deschere i need delete productdata productnumbers same in both tables , specify variables accountid is, , productowner is.
e.g know accountid 100001 , productowner tom. therefore want rows 1 , 2 in productdata table deleted only.
edit: believe may have cracked query i've been working on
mysql_query("delete productdata.* productdata inner join allproducts on productdata.productnumber = allproducts.productnumber (productdata.accountid = '100001' , allproducts.productowner = 'tom')"); i've done quick test , seems work - thoughts/criticisms?
your use of mysql_query deprecated of php 5.5.0 , removed in future. should start using mysqli or pdo_mysql extension.
i further suggest store query:
delete productdata.* productdata inner join allproducts on productdata.productnumber = allproducts.productnumber (productdata.accountid = '100001' , allproducts.productowner = 'tom' in stored procedure on database.
when using pdo's example, can call follows:
$db = new pdo('mysql:host=xxx;port=xxx;dbname=xxx', 'xxx', 'xxx', array( pdo::attr_persistent => false)); // sure cleanse passed in arguments! $stmt = $db->prepare("call deleteproductdata($accountid, $productowner)"); // call stored procedure $stmt->execute(); stored procedure example:
create definer=`root`@`localhost` procedure `deleteproductdata`(in `accountid` bigint, in `productowner` varchar(128)) language sql not deterministic contains sql sql security definer comment '' begin delete productdata inner join allproducts on productdata.productnumber = allproducts.productnumber productdata.accountid = accountid , allproducts.productowner = productowner; end this way moving mysql code out of php , database, belongs.
Comments
Post a Comment