I have 3 tables: articles:

id title content date

tags:

id  name

tags_in_news:

id  news_id  tag_id

news_id is foreign key for articles table and tag_id is foreign key for tags tables...How to delete an article?? I tried but did not receive,my code:

$aid=(int)$this->uri->segment(3,0);
 $this->db->query("DELETE * FROM articles, tags_in_news WHERE articles.id = $aid AND tags_in_news.news_id = $aid ");

Help me please ;)

有帮助吗?

解决方案 2

You can define foreign key constraints on the tables with ON DELETE CASCADE option.

Then deleting the record from parent table removes the records from child tables.

DELETE articles a ,tags_in_news t FROM articles INNER JOIN tags_in_news  WHERE a.id = t.id AND a.id = $aid

ref : Mysql - delete from multiple tables with one query

其他提示

Try this:

DELETE a, tn 
FROM articles a INNER JOIN 
     tags_in_news  tn
WHERE a.id=tn.news_id 
    AND a.id = $aid 
    AND tn.news_id = $aid 

Another option:

You can define foreign key constraints on the tables using ON DELETE CASCADE.

Then deleting the record from parent table removes the records from child tables. Read more here.

Credit: SO answer.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top