gpt4 book ai didi

php - 选择然后从数据库中删除

转载 作者:行者123 更新时间:2023-11-29 01:43:25 25 4
gpt4 key购买 nike

我正在努力从数据库中删除,而不是加入表,我选择 costume_id(主键)并尝试以这种方式删除,但它似乎只从一个表而不是所有表中删除。我曾尝试删除主键之前的外键,但效果不佳。我必须加入表格然后删除还是我的方式有效?

   $select =  $mysqli->query("SELECT * FROM costumes WHERE $q");
while ($row = $select->fetch_assoc())
$db_id = $row["costume_id"];

$costume = $mysqli->query("DELETE FROM costumes WHERE costume_id='$db_id'");
$costume_row = $costume->fetch_assoc();

$history = $mysqli->query("DELETE FROM history WHERE history_id='$db_id'");
$history_row = $history->fetch_assoc();

$location = $mysqli->query("DELETE FROM location WHERE location_id='$db_id'");
$location_row = $location->fetch_assoc();

$state = $mysqli->query("DELETE FROM state WHERE state_id='$db_id'");
$state_row = $state->fetch_assoc();

$q 是一个子流。在我的表中有:

costumes (
costume_id varchar(10) primary key,
name varchar(50),
is_seperate bool,
is_onepeice bool,
description text,
color varchar(25),
material varchar(50),
genre varchar(25),
gender char(1),
size varchar(5)
);
state (
state_id varchar(10),
in_use bool,
fixed bool,
condition text,
foreign key (state_id) references costume(costume_id)
);
history (
history_id varchar(10),
previous_user varchar(20),
profession varchar(20),
previous_play varchar(50),
date date, fixed_previous bool,
comments text,
foreign key (history_id) references costume (costume_id)
);
location (
location_id varchar(10),
loc_no varchar(10) primary key,
room_no varchar(3),
foreign key (location_id) references costumes (costume_id)
);

我的新删除查询是:

 $delete = $mysqli->query("DELETE costumes,history,status,location FROM costumes join   history on costumes.costume_id = history.history_id join status on status.status_id join location on location.location_id where costume_id='$db_id'");

那个查询给我带来了约束问题

最佳答案

您应该在删除语句中连接您的表。所以完成这个查询然后尝试一下:

DELETE costumes, history
FROM costumes JOIN history ON costume.costume_id = history.costume_id
WHERE costume_id = '$db_id';

由于我不知道您的表的确切结构,您必须更正连接语法以匹配您通常连接它们的方式。如需进一步帮助,请尝试编辑您的问题并向我们提供您的 2 个表格中的所有字段名称。

关于php - 选择然后从数据库中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13718262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com