简体   繁体   中英

what's wrong with this query in SQL

I have the following query:

UPDATE PRODUCT SET FIXEDCOST = 
   (Select PRICE from PRODUCTPROGRAM 
    where productID = PRODUCTID) * .6 
WHERE FAMILY = 'Services'

I need to update the PRODUCT table's FIXEDCOST field with 60% of the value of Price in the PRODUCTPROGRAM table.

The tables are related by productID .

The error says that more than one is returned in the subquery. Any ideas as to how I could fix this? Thanks in advance for any help.

Tamer, you are getting too many results in the "select price from proudctprogram" you need narrow it down or do something like this:

UPDATE PRODUCT A, PRODUCTPROGRAM B   
SET A.FIXEDCOST = B.PRICE * .6 
WHERE A.productID = B.PRODUCTID AND FAMILY ='Services'

Try that and let me know. Marcelo

虽然我不确定语法是否为100%,但您可能想首先尝试引用每个productId设置为PRODUCT.productid和PRODUCTPROGRAM.PRODUCTID的表。

The nested query will return all records from the PRODUCTPROGRAM table, as all records have a PRODUCTID value that is equal to itself.

You have to specify that you want to compare it to the value in the PRODUCT table:

UPDATE PRODUCT
SET FIXEDCOST =
  (Select PRICE from PRODUCTPROGRAM 
  where productID = PRODUCT.PRODUCTID) * .6 
WHERE FAMILY = 'Services'
UPDATE PRODUCT p
    JOIN PRODUCTPROGRAM pp ON pp.productID = p.productID
SET p.FIXEDCOST = pp.PRICE *.6
WHERE p.FAMILY = 'Services'
UPDATE PRODUCT SET FIXEDCOST = 
   (Select TOP 1 PRICE from PRODUCTPROGRAM 
    where productID = PRODUCT.PRODUCTID) * .6 
WHERE FAMILY = 'Services'

Your sub query must be returning more than one value.

Select PRICE from PRODUCTPROGRAM 
where productID = PRODUCTID

Try limiting the subquery or use the unique key in the where clause:

    UPDATE PRODUCT SET FIXEDCOST = 
   (Select PRICE from PRODUCTPROGRAM 
    where productID = PRODUCTID LIMIT 1) * .6 
WHERE FAMILY = 'Services'

Use table aliases to avoid ambiguity? Like UPDATE PRODUCT P SET FIXEDCOST = (SELECT PRICE FROM PRODUCTPROGRAM PP WHERE P.PRODUCTID = PP.PRODUCTID) ...

I suspect the where-statement is wrong. You mean, probably:

where productID = PRODUCT.PRODUCTID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM