MySQL删除 - 选择行

时间:2015-06-13 18:47:34

标签: mysql delete-row

我正在尝试从表订阅中删除一行,其中有两个外键( id_user id_journal )。我拥有的信息是来自表用户电子邮件和来自表 journal nome 。已删除的行需要与 user.email journal.nome 匹配。我找不到解决方案。我该怎么办?

表用户:

id
name
email
password

表日记:

id
name

表订阅:

id
id_user
id_journal

我尝试的最后两个查询:

DELETE FROM assinatura WHERE (
SELECT  tbluser.id, journal.id
FROM    tbluser, journal
WHERE email = '$email' AND nome = '$nome')

DELETE FROM assinatura 
INNER JOIN tbluser on (tbluser.email = '$email')
INNER JOIN journal on (journal.nome = '$nome')

我尝试了很多其他查询,但都没有成功。我认为说我是MySQL的新手很重要。

2 个答案:

答案 0 :(得分:0)

DELETE 
FROM Subscription
WHERE id_user IN (
   SELECT      usr.id 
   FROM        user AS usr 
   WHERE       usr.email = INPUT_EMAIL
) 
AND id_journal IN (
   SELECT      jrnl.id 
   FROM        journal AS jrnl 
   WHERE       jrnl.name = INPUT_NAME
)

答案 1 :(得分:0)

另一个话题......

尝试通过

避免对同一用户/期刊组合的多余订阅
CREATE TABLE subscription
(
  id int NOT NULL AUTO_INCREMENT primary key,
  id_user int not null,
  id_journal int not null,
  UNIQUE KEY `user_journal_intersect` (`id_user`,`id_journal`)
  -- note Alan stated FK RI in place already
);

U可以在复合上使用PK,当然(放弃id列),程序员首选