2

I wanted a guidance for one problem . Suppose i have one table alphabets (alpha, id).
In column alpha having value a-z and in column id all values are 1.

a  1
b  1
.  .
.  .
z  1

Now i want a database query to insert data like a-z but with id=2 .

Can anybody help me out .

1 Answer 1

6
INSERT INTO alphabets (alpha, id)
SELECT alpha, 2
  FROM alphabets a
 WHERE id = 1
   AND NOT EXISTS (
        SELECT * FROM alphabets
         WHERE alpha = a.alpha
           AND id = 2
       )

Note that the WHERE clause makes this query idempotent, i.e., it won't duplicate the id=2 rows if you run it twice.

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.