Mysql在select存在的地方插入


Mysql insert where select exist

我的mysql查询需要您的帮助。我试着在产品中插入货币(如果有的话)。在php代码中,此查询显示如下

$q = "select * from currency where type = ".$input." limit 1";
    if(mysql_query($q)){
    $q2= "insert into product ("cur","cur_type","val") VALUE ($q['id'], $q['type'],$price)";
    if(mysql_query($q2)){
    echo "success";
    }
    }

就像这样,但是我如何在一个查询中生成这些代码呢?你能帮我写一个查询吗?谁在其他表中存在货币的地方插入输入值?感谢

您可以使用insert。。。选择语句来插入select。请注意,您不应该再使用mysql_*()函数了,它们已被弃用。请改用mysqli或pdo。

sql看起来像sg,如下所示:

insert into product (cur,cur_type,val)
select id, type, $price from currency where type=$input

但是,您需要编写代码的php部分。

其中一个选项是使用这样的查询。

"INSERT INTO pruduct (cur,cur_type,val)
SELECT id,type, {$price} AS embeded_price FROM currency WHERE type='{$input}'LIMIT 1";