0

I want to insert value that i get with SELECT to table, but i have no idea how to format it so it's work. When i try the SELECT statement alone, it returns value that i want, but when its nested it returns 0 which it shouldn't. I tried wrapping it in "" '' but it doesn't work.

INSERT
INTO
  brand(brand_id,
  NAME,
  cat_id)
VALUES(
  NULL,
  "value",
  (
  SELECT
    cat_id
  FROM
    category
  WHERE
    cat_id = 'number'
)
)

1 Answer 1

1

Just use insert . . . select. Leave out the values:

INSERT INTO brand(brand_id, NAME, cat_id)
  SELECT NULL, 'value', cat_id
  FROM category
  WHERE cat_id = 'number';

Three comments:

  • If number is really a number, then don't use quotes.
  • This is slightly different from your version because it will insert no rows if there are no matches.
  • If the subquery returns exactly one row, then your version should work.
Sign up to request clarification or add additional context in comments.

1 Comment

It works, thanks. The select statement confused me a bit because i didn't know i can put values that are not even in table there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.