PHP &MySQL - 如何从两个表中删除用户记录 [英] PHP & MySQL - How to delete a users records from two tables
问题描述
您好,我是 PHP 的新手 &MySQL,我想知道如何使用 PHP & 从下面这两个表中删除所有 user_id 3
记录.MySQL?
Hello I'm new to PHP & MySQL and I was wondering how can I delete all of user_id 3
records from these two tables below using PHP & MySQL?
我的 MySQL 表
CREATE TABLE ls (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
skill VARCHAR(255) DEFAULT NULL,
experience VARCHAR(22) DEFAULT NULL,
self_grade VARCHAR(10) DEFAULT NULL,
date_created DATETIME NOT NULL,
date_updated DATETIME DEFAULT NULL,
PRIMARY KEY (id)
);
CREATE TABLE us (
id INT(13) UNSIGNED NOT NULL AUTO_INCREMENT,
skill_id INT(13) UNSIGNED NOT NULL,
user_id INT(13) UNSIGNED NOT NULL,
PRIMARY KEY (id)
);
us 表值
id skill_id user_id
9 7 3
10 8 3
11 9 3
12 10 3
13 17 6
ls 表值
id skill experience self_grade date_created date_updated
7 html 5 years A 2010-10-19 07:11:08 2010-10-19 07:12:06
8 jquery 10 years B 2010-10-19 07:11:27 2010-10-19 07:12:06
9 css 1 year NULL 2010-10-19 07:11:38 2010-10-19 07:12:06
10 php 2 years C 2010-10-19 07:13:05 NULL
17 php 2 years C 2010-10-19 07:19:05 NULL
推荐答案
您可以获取属于 user_id = 3
的所有行,然后使用生成的WHERE
子句.
You could fetch all rows belonging to user_id = 3
and then build a DELETE
statement with a generated WHERE
clause.
但是,我想知道您是否有不合适的表设计,因为您在用户和技能之间存在 m:n 关系,并且您想删除与 ID 为 3 的用户相关的所有技能.但这意味着用户和技能之间存在 1:m 关系(1 个用户拥有多项技能,而不是与他人共享技能).或者我做错了什么......?
But, however, I'm wondering if you have an inappropriate table design as you have a m:n relation between user and skill and you want to delete all skills the user with id 3 is related with. But this implicates a 1:m relation between user and skill (1 user has multiple skills, not sharing the skills with others). Or maybe I got something wrong...?
这篇关于PHP &MySQL - 如何从两个表中删除用户记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!