1

i'm trying to use INSERT into with SELECT DISTINCT and this is what i tried so far.

INSERT INTO creature_loot_template VALUES (id, 68910, 0, 100, 1, 1, 0, 1, 1, NULL)
SELECT DISTINCT id FROM creature WHERE map = 389;

So this is example of what it should do (there is more than 20 - 40 lines:

INSERT INTO creature_loot_template VALUES (123, 68910, 0, 100, 1, 1, 0, 1, 1, NULL);
INSERT INTO creature_loot_template VALUES (124, 68910, 0, 100, 1, 1, 0, 1, 1, NULL);
INSERT INTO creature_loot_template VALUES (125, 68910, 0, 100, 1, 1, 0, 1, 1, NULL);
INSERT INTO creature_loot_template VALUES (125, 68910, 0, 100, 1, 1, 0, 1, 1, NULL);

I've also tried the following SQL's :

INSERT INTO creature_loot_template VALUES ((SELECT DISTINCT id FROM creature WHERE map = 389), 68910, 0, 100, 1, 1, 0, 1, 1, NULL);
SET
@id = (SELECT DISTINCT id FROM creature WHERE map = 389);
INSERT INTO creature_loot_template VALUES (@id, 68910, 0, 100, 1, 1, 0, 1, 1, NULL);
3
  • 2
    INSERT INTO creature_loot_template SELECT DISTINCT id, 68910, 0, 100, 1, 1, 0, 1, 1, NULL FROM creature WHERE map = 389; Commented Jan 30, 2020 at 12:38
  • Does this answer your question? sql insert into table from select without duplicates (need more then a DISTINCT) Commented Jan 30, 2020 at 12:39
  • Yes that does help, i have saw already but i wasn't sure on how it should work but now i did so thanks Commented Jan 30, 2020 at 12:43

1 Answer 1

2

Just list the values you want on the SELECT:

INSERT INTO creature_loot_template
    SELECT DISTINCT id, 68910, 0, 100, 1, 1, 0, 1, 1, NULL
    FROM creature
    WHERE map = 389;

I would recommend that you list all the columns in the INSERT as well. This ensures that the code does what you actually want.

Sign up to request clarification or add additional context in comments.

4 Comments

I hope you didn't do the downvote, else your answer has solved my issue i'll accept whenever i can thank you
@JadaDeveloper . . . As someone who never downvotes, you can be assured that I did not downvote. I find that comments are more useful. And in cases where quality is really low, I just vote to close.
DISTINCT is probably redundant.
@RickJames . . . There is no indication in the question of whether there could be duplicates for a single map. I do agree that it is probably redundant, but the OP has it in the question. That comment should really be directed to the OP.

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.