Jump to content

Recommended Posts

I am trying to write a MySQL INSERT statement which has a SELECT query. Select query can result in NULL. If result is NULL, I want to 1 to be the value for the field. If result is not NULL, I want retrieved value. Below is the MySQL I have coded but it seems wrong. What would be the right statement

 

INSERT  
INTO table        
(         
primary_id        
,sequence_number        
,text        
,text2        
,text3      
)select          
30         
,((SELECT 
MAX(sequence_number) + 1 FROM table     
WHERE primary_id = 30) 
AS seq_num 
IF (seq_num 
= NULL,1,seq_num))         
,'text'         
,'text2'
,'text3
 

 

Thank you. That worked. So, the script that worked is below, for other's reference.

 

INSERT
  INTO table        (
         primary_id
        ,sequence_number
        ,text

        ,text2

        ,text3
      )
select
          30
         ,(SELECT ifnull ((MAX(sequence_number) + 1), 1)
      FROM table
     WHERE primary_id = 30)
         ,'text'

         ,'text2'

         ,'text3

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.