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

时间:2014-12-12 09:28:03

标签: php mysql database

我尝试过很多R& D但这个问题可以解释我的问题。 我不知道我做错了什么。

select color_code from color_master WHERE color_code like CONCAT('%','(select color_code from artist_upload_painting where painting_stock_status = 1)','%')

我想从master表中删除一行,其中color_code位于表artist_upload_painting的color_code字段中,其中painting_stock_status = 1。

7 个答案:

答案 0 :(得分:4)

使用如下(子查询)

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

答案 1 :(得分: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");

        }
    }

答案 2 :(得分:0)

尝试:

DELETE 
FROM master 
WHERE color_code IN ( SELECT DISTINCT color_code
                     FROM artist_upload_painting
                     WHERE painting_upload_status = 1)

答案 3 :(得分:0)

加入应该更有效率

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

如果您经常运行此查询,则可以在artist_upload_paintingcolor_code列的painting_stock_status表中添加索引,以加快此过程。

答案 4 :(得分:0)

$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或mysqli *模型并尝试进入OOP。

答案 5 :(得分:0)

试试这个:

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);

答案 6 :(得分:0)

@Abhimanu

我在phpMyAdmin中使用了这段代码。设置@var =“SELECT * FROM artist_upload_painting WHERE painting_stock_status ='1'”; SELECT * FROM color_master WHERE color_code LIKE @var;但是这段代码返回一个空集。

请做这个。 @var =“SELECT * FROM artist_upload_painting WHERE painting_stock_status ='1'”而不是*使用与颜色代码匹配的特定列名称