Delete two ID's from different tables in the same query

0

Good afternoon! Home I have two tables: 1st questionnaire and 2nd response , and question_id is the key that allows you to interconnect the two tables, foreign key in table "answer" of the "questionnaire" table. Home When I enter a record, I can not delete the record first from the "questionnaire" table, but first from the "comment" table. Home As I developed a page the administrator can see all the results of the database, and manipulate them. One of the options is to delete the results, but I'm not able to a query to delete the last ID from your tables ...


The ID I get by the URL:
$resposta_id = $_GET['resposta_id']; Home but only the ID of the response " $resposta_id ", in query I thought I would somehow get the last ID of the "questionnaire" table in a decreasing way, but I could not form a query only with all that, and for the junction of the tables I had made:

select * from questionario left join resposta on questionario.pergunta_id=resposta.pergunta_id


If someone has an idea how to do it, or even another way, thank you!

    
asked by anonymous 26.06.2018 / 16:40

1 answer

2

Since pergunta_id is a fk of tables, you can INNER JOIN directly in delete .

DELETE t1,t2 FROM t1
    INNER JOIN
    t2 ON t2.ref = t1.id 

In this example it will delete all records where t2.ref is equal to t1.id .

Having the value of pergunta_id simply add the clause WHERE

DELETE t1,t2 FROM t1
    INNER JOIN
    t2 ON t2.ref = t1.id 
    WHERE t1.id = '{pergunta_id}'

See the working query: link

    
26.06.2018 / 16:55