Php 通过从mysql中的另一个表获取数据来删除表中的数据

Php 通过从mysql中的另一个表获取数据来删除表中的数据,php,mysql,database,Php,Mysql,Database,我尝试了很多研发,但这个问题可以解释我的问题。 我不知道我做错了什么 select color_code from color_master WHERE color_code like CONCAT('%','(select color_code from artist_upload_painting where painting_stock_status = 1)','%') 我想从主表中删除一行,该行的颜色\u代码位于表艺术家\u上传\u绘画的颜色\u代码字段中,其绘画\u库存\u状态为1

我尝试了很多研发,但这个问题可以解释我的问题。 我不知道我做错了什么

select color_code from color_master WHERE color_code like CONCAT('%','(select color_code from artist_upload_painting where painting_stock_status = 1)','%')
我想从主表中删除一行,该行的颜色\u代码位于表艺术家\u上传\u绘画的颜色\u代码字段中,其绘画\u库存\u状态为1。

使用如下子查询

delete from color_master WHERE color_code  in (select color_code from artist_upload_painting where painting_stock_status = 1);
尝试:


加入应该更有效

DELETE FROM color_master 
JOIN artist_upload_painting
ON color_master.color_code = artist_upload_painting.color_code
AND artist_upload_painting.painting_stock_status = 1
如果您经常运行此查询,您可以在“艺术家上传绘画”表中的“颜色代码”和“绘画股票”状态列中添加索引,以加快此过程

$queryToFindMatching = mysql_query("SELECT * FROM artist_upload_painting WHERE painting_upload_status = '1'");

while ($resultOfMatching = mysql_fetch_assoc($queryToFindMatching)){

    mysql_query("DELETE FROM color_master WHERE color_code = '{$resultOfMatching['color_code']}'");

}
不过,您确实不应该使用mysql_*函数。。可以考虑PDO或MySQL模型,尝试进入OOP。

< P>尝试:< /P>
DELETE
FROM color_master CM
WHERE CM.color_code IN (
    SELECT AUP.color_code
    FROM artist_upload_painting AUP
    WHERE AUP.painting_stock_status = 1);
@阿披马努


我在phpMyAdmin中使用了此代码。设置@var=SELECT*FROM artist_upload_painting,其中painting_stock_status='1';从color_master中选择*,其中的color_代码类似于@var;但此代码返回一个空集

请做这个。
@var=SELECT*FROM artist\u upload\u painting WHERE painting\u stock\u status='1'而不是*使用与颜色代码匹配的特定列名

我自己解决了问题。这是解决办法

$del_Query = mysql_query("select DISTINCT(color_code) from artist_upload_painting where upload_painting_id = '$itemid'");
    while($Get_Result = mysql_fetch_array($del_Query))
    {
    $master_color = explode(",",$GetResult['color_code']);
    $uniq_color = array_unique($master_color);
        foreach ( $uniq_color as $color)
        {
            mysql_query("delete from color_master where color_code = $color");

        }
    }


为什么我看到两个select?为什么不使用IN子句?从color_master中选择color_code,其中从Artister_upload_Paint中选择color_code,其中Paint_stock_status=1,因为table color_master中的color_code类似于182461,table Artister_upload_Paint中的color_code类似于173a3c、81876c、B0B191405E5A、565025、7a9ca0、4A382066563E、a3ac8c,.首先,我想看看它是否返回一些数据。所以我尝试了这个代码。选择*从“颜色”主控形状中选择“颜色”代码从“艺术家”中选择“不同的颜色”代码从“上传”中选择“绘画”从“绘画”中选择“库存”状态=1是否返回任何内容?否,返回的是空集。绘画表中是否有“绘画”上传状态=1且主控形状中有该颜色代码的行表?重新阅读我的问题“绘图上传”不是一个字段。在实现此代码之前,我可以使用SELECT查询查看结果吗。所以我尝试了这个,选择*FROM color\u master JOIN artist\u upload\u painting ON color\u master.color\u code=artist\u upload\u painting.color\u code和artist\u upload\u painting.painting\u stock\u status=1当我使用select时,它返回一个空集。首先我想看看它是否返回一些数据。所以我尝试了这个代码。选择*FROM color_master WHERE color_code IN select DISTINCT color_code FROM artist_upload_painting WHERE painting_stock_status=1如果table color_master中的color_code类似于182461,而table artist_upload_painting中的color_code类似于173a3c、81876c、B0B191405E5A,565025,7a9ca0,4A382066563E,a3ac8c,.从颜色母版中删除颜色代码,如“{$resultOfMatching['color\u code']}”@abhimanu我可以使用删除前选择查看此查询的结果吗?从颜色母版中选择*,其中颜色代码,如“{$resultOfMatching['color\u code']}”,我在phpMyAdmin中使用了此代码。设置@var=SELECT*FROM artist_upload_painting,其中painting_stock_status='1';从color_master中选择*,其中的color_代码类似于@var;但是这个代码返回一个空集。这个代码仍然返回一个空集。此查询返回1个结果。选择*从艺术家上传绘画,其中绘画库存状态='1';我不明白为什么这个查询返回一个空集,请解释mysql的更多数据结构,这两个查询的结构是:表artist\u upload\u painting拥有artist的所有数据,upload\u painting\u id是自动递增和主键,color\u id[int],color\u code[varchar],upload\u painting\u status[0或1],painting\u stock\u status[0或1]表color\u master有两个字段,例如color\u id[int autoincrement],color\u code[varchar]。现在请检查这一个:$colorcode=选择艺术家上传的color\u code\u painting,其中painting\u stock\u status=1$colormasterarray=从color\u master中选择*,其中color\u code=.$colorcode;
$del_Query = mysql_query("select DISTINCT(color_code) from artist_upload_painting where upload_painting_id = '$itemid'");
    while($Get_Result = mysql_fetch_array($del_Query))
    {
    $master_color = explode(",",$GetResult['color_code']);
    $uniq_color = array_unique($master_color);
        foreach ( $uniq_color as $color)
        {
            mysql_query("delete from color_master where color_code = $color");

        }
    }