开发者

MySQL, php table question (delete on an update)

开发者 https://www.devze.com 2023-03-22 05:33 出处:网络
I h开发者_StackOverflow社区ave the two following tables table A id | name | 1| bob | 2| jill |

I h开发者_StackOverflow社区ave the two following tables

table A

| id | name |
| 1  | bob |
| 2  | jill |
| 3  | jojo |

Table A is displayed by using checkboxes.

On the first go, the user checks all three checkboxes so you get the result in table B.

table B

| table_a_id | table_c_id |
| 1          |   2        |
| 2          |   2        |
| 3          |   2        |

But the next time the user goes to edit, they UNCHECK '2' so that it's only:

1
3

How do I write my query (using either mySQL or php) so that TABLE B is updated to:

| table_a_id | table_c_id |
| 1          |   2        |
| 3          |   2        |


DELETE A,B 
FROM A
LEFT JOIN B
ON B.table_a_id = A.id
WHERE A.id NOT IN (1,3)

Or use InnoDB with a Foreign key ON DELETE CASCADE, much simpler :)


DELETE 
FROM TableB 
WHERE (NOT table_a_id IN (1, 3)) 
    AND (table_c_id = 2)
0

精彩评论

暂无评论...
验证码 换一张
取 消