我正在使用php和mysql的混合来执行以下查询,我确信必须有一种方法可以在SQL中执行此操作。我已经删除了一些代码并留下足够的内容,你应该能够看到我正在努力实现的目标。
非常感谢任何帮助!
SELECT result_id FROM categories
WHERE name = "conventional"
foreach ( $results as $row ){
$query = "
DELETE FROM categories
WHERE result_id = $row->result_id && (name = 'va' OR name = 'fha' OR name = 'usda' OR name = 'other')
";
$this->db($query);
}
编辑:从我到目前为止收到的答案来看,我认为我已经解释得不够好了。此表中的列是id,result_id,name
有多个条目共享相同的result_id,因为它们与“results”表中的条目相关。所以我试图找到名为“常规”的条目的result_id,所以我可以找到具有相同result_id的所有条目,如果它们的名称是“va”,“fha”,“usda”或“other”,则删除它们
答案 0 :(得分:3)
DELETE FROM categories
WHERE name IN ("va", "fha", "usda", "other")
AND result_id IN (
SELECT result_id FROM categories
WHERE name = 'conventional'
)
答案 1 :(得分:0)
您希望避免在循环中删除查询,以下是如何执行此操作:
SELECT result_id FROM categories
WHERE name = "conventional"
// Now build csv of ids
$arx = array();
foreach ( $results as $row ) {
$arx[] = $row->result_id;
}
if(!empty($arx)) {
$arx_1 = implode(',', $arx);
$query = "DELETE FROM categories
WHERE result_id in ({$arx_1}) AND (name = 'va' OR name = 'fha' OR name = 'usda' OR name = 'other')";
$this->db($query);
}