基本上,我有两张表与1对1的关系
当我从subset_panel_options中删除一行时,我还想删除subset_panel_options_list中的相关行
这是两个表的一些结构。没有必要显示完整的表格。
[subset_panel_options]
- > ID
- > subset_panel_id
[subset_panel_options_list]
- > ID
- > subset_panel_options_id
DELETE subset_panel_options, subset_panel_options_list
FROM subset_panel_options
JOIN subset_panel_options_list
WHERE subset_panel_options.id = subset_panel_options_list.subset_panel_options_id
AND subset_panel_id = $subsetPanelId
答案 0 :(得分:1)
是的,它可以:
DELETE FROM subset_panel_options
LEFT JOIN subset_panel_options_list
ON (subset_panel_options.id = subset_panel_options_list.subset_panel_options_id)
WHERE subset_panel_options.subset_panel_id = $subsetPanelId
使用LEFT JOIN
确保您从“subset_panel_options”中删除,即使subset_panel_options_list表中没有相应的匹配项。
您可能还希望使用InnoDB引擎中提供的参照完整性功能。在这种情况下,您需要在subset_panel_options_id
表中将subset_panel_options_list
定义为FK(外键),并在其上定义“ON DELETE CASCADE”约束,这意味着当subset_panel_options
处的行时删除后,subset_panel_options_list
中的“孤儿”行也应立即删除。