我有一张简单的牌桌:
CREATE TABLE `users_individual_cards` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` char(36) NOT NULL,
`individual_card_id` int(11) NOT NULL,
`own` int(10) unsigned NOT NULL,
`want` int(10) unsigned NOT NULL,
`trade` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `user_id` (`user_id`,`individual_card_id`),
KEY `user_id_2` (`user_id`),
KEY `individual_card_id` (`individual_card_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
我有ajax来添加和删除基于OWN,WANT和TRADE的记录。但是,如果用户删除了所有OWN,WANT和TRADE卡,它们将归零,但会将记录留在数据库中。我希望删除记录。在每次“更新”后检查是否所有列= 0是唯一的方法吗?或者我可以使用以下内容设置条件触发器:
//psuedo sql
AFTER update IF (OWN = 0, WANT = 0, TRADE = 0) DELETE
最好的方法是什么?你能帮忙解决这个问题吗?
答案 0 :(得分:1)
为什么不从PHP(或其他前端)发出两个查询?
update `users_individual_cards` ...
delete `users_individual_cards` where ... (same condition) and own + want + trade = 0
答案 1 :(得分:1)
触发器将是:
CREATE TRIGGER users_individual_cards_trigger
AFTER UPDATE ON users_individual_cards
FOR EACH ROW
BEGIN
DELETE FROM users_individual_cards
WHERE 'OWN' = 0 AND 'WANT' = 0 AND 'TRADE' = 0;
END$$
解决方案抛出删除查询会更好,因为并非所有版本的mysql都支持它。