0

如何在一个单一的 SQL 语句中制作这个 PHP 脚本?

$sql = 'SELECT oxtprice from oxarticles where oxparentid = ? and nrseriesarticle = 1';
        $price = DatabaseProvider::getDb()->getOne($sql, [$id]);

        if ($price) {
            $updateSql = "Update oxarticles SET nrseriestprice = ? WHERE oxid = ? and oxparentid = ''";
            DatabaseProvider::getDb()->execute($updateSql, [$price, $id]);

我想要这样的东西,但它没有用

UPDATE oxarticles SET 
nrseriesprice = (SELECT oxprice from oxarticles where oxparentid = ? and nrseriesarticle = 1) 
WHERE oxid = ?
4

1 回答 1

1

您只需要一个查询,如下所示

CREATE TABLE parent (oxid int ,nrseriesprice DECIMAL (10,2))
INSERT INTO parent VALUES (1,NULL)
CREATE TABLE oxarticles (oxprice DECIMAL(19,2), oxparentid int,nrseriesarticle int)
INSERT INTO oxarticles VALUES (19.2,1,1)
UPDATE parent p SET 
nrseriesprice = (SELECT oxprice from oxarticles where oxparentid = p.oxid and nrseriesarticle = 1) 
WHERE p.oxid = 1
SELECT * FROM parent
氧化 | nrseries价格
---: | ------------:
   1 | 19.20

db<>在这里摆弄

于 2022-02-11T12:05:20.193 回答