0

I am writing one simple SQL query to update my table .

here is my code

$qry = mysql_query("update product set cat_tree =(select cat_name from cat_details where product.category = cat_details.cat_id )");

When ever i tried to run this code its giving error message

#1242 - Sub query returns more than 1 row

What is the problem ? I tried with so many solutions like "IN" "ANY" . But all examples are for insert query only.

Thanks in advance

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
mani
  • 436
  • 3
  • 13

3 Answers3

0

Try this

update product p join cat_details c on p.category = c.cat_id
set p.cat_tree = c.cat_name
where p.category =  c.cat_id
Nawaz
  • 303
  • 1
  • 4
  • 15
0

You should try LIMIT like this...

$qry = mysql_query("update product set cat_tree =(select cat_name from cat_details where product.category = cat_details.cat_id LIMIT 1)");
0

What you are looking for is an UPDATE query with a JOIN (Ref):

UPDATE product AS p 
  JOIN cat_details AS c
  ON p.category = c.cat_id 
SET p.cat_tree = c.cat_name;
Community
  • 1
  • 1
Reversal
  • 622
  • 5
  • 19