This is a discussion on Conditional INSERT within the MySQL Database forums, part of the Database Forums category; Hello, I'm trying to perform an INSERT .. but only if no rows in the table have a specific field ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hello, I'm trying to perform an INSERT .. but only if no rows in the table have a specific field that equals zero. For example: Nodes has the fields NodeID and ParentNodeID (there are more than that, but this is simplified) NodeID will be unique no matter what, since it is incremental. I want to INSERT INTO Nodes VALUES(NULL, 0) only if // There are no Nodes that have a ParentNodeID of 0 SELECT COUNT(NodeID) FROM Nodes WHERE ParentNodeID = 0 is less than 1 Thank you |
|
|||
|
On Jun 25, 2:19 pm, evanp...@gmail.com wrote:
> Hello, > > I'm trying to perform an INSERT .. but only if no rows in the table > have a specific field that equals zero. > > For example: > > Nodes has the fields NodeID and ParentNodeID (there are more than > that, but this is simplified) > > NodeID will be unique no matter what, since it is incremental. > > I want to > INSERT INTO Nodes > VALUES(NULL, 0) > > only if > // There are no Nodes that have a ParentNodeID of 0 > SELECT COUNT(NodeID) > FROM Nodes > WHERE ParentNodeID = 0 is less than 1 > > Thank you if not exists (select 1 from nodes where ParentNodeID = 0) begin insert into Nodes values(null, 0); end if; |
![]() |
| Thread Tools | |
| Display Modes | |
|
|