我有一个包含文章的表,另一个包含文章的行
一旦删除了给定行中的所有文章,我也需要删除该行。
这是我目前的查询
SET @id = :id;
SET @section = 0;
SET @row = 0;
SET @row_id = 0;
SET @count = 0;
SELECT @row := articles.row_order, @section := articles.section_id
FROM articles
WHERE article.article_id = @id;
DELETE FROM articles WHERE article_id = @id;
SELECT @count := COUNT(articles.article_id)
FROM articles
JOIN rowstyles ON articles.section_id = rowstyles.row_section_id
AND articles.row_order = rowstyles.row_position
WHERE rowstyles.row_section_id = @section AND rowstyles.row_position = @row;
DELETE ...
如果@count为0(零)
,如何删除行(行样式)答案 0 :(得分:3)
您可以将计数添加到DELETE
s where子句:
-- Delete rowstyles with no article
DELETE FROM rowstyles WHERE (SELECT COUNT(a.id) FROM articles a WHERE a.section_id = rowstyles.row_section_id) = 0;
请注意,这将删除所有没有文章的行样式。如果它仅用于给定的部分,则可以添加到WHERE子句:
DELETE FROM rowstyles
WHERE (
SELECT COUNT(a.article_id)
FROM articles a
WHERE
a.section_id = @section AND a.row_order = @row) = 0
AND
rowstyles.row_section_id = @section AND rowstyles.row_position = @row;
在运行SQL之前,请先确保备份。