2

I have recursive table:

CREATE TABLE IF NOT EXISTS `Table1` (
   `Id_table1` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
   `Name` VARCHAR(45) NOT NULL ,
   `Parent_Id` INT UNSIGNED NOT NULL ,
   PRIMARY KEY (`Id_table1`) ,
   INDEX `fk_Table1_Table1` (`Parent_Id` ASC) ,
   UNIQUE INDEX `Id_UNIQUE` (`Id_table1` ASC) ,
   CONSTRAINT `fk_Table1_Table1`
      FOREIGN KEY (`Parent_Id` ) REFERENCES `Table1` (`Id_table1` )
      ON DELETE NO ACTION
      ON UPDATE NO ACTION)
   ENGINE = InnoDB;

I try to execute:

INSERT INTO table1 (`Name`, `Parent_Id`)
   VALUES ('name123', SELECT Id_table1
                      from table1
                      where table1.`Name` = 'sampleName')

Unfortunately, receives an error.

Data table:

id: 1 name: name1   parent_id: null
id: 2 name: name11  parent_id: null
id: 3 name: name123 parent_id: null

Any idea how to execute this insert?

2
  • is the sampleName a value in table1? before the insert and has it been committed so that it can be selected? Commented Dec 3, 2011 at 17:49
  • It would also help when you have a question about a query that throws an error, that you also include the error with your question. Commented Dec 3, 2011 at 17:50

1 Answer 1

5

Try the following INSERT:

INSERT INTO table1 (Name, Parent_Id)
SELECT 'name123', t.Id_table1
FROM table1 AS t
WHERE t.Name = 'sampleName';
Sign up to request clarification or add additional context in comments.

Comments

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.