INSERT INTO NOT EXISTS

1

I need to create an INSERT where it is checked whether the value already exists. I looked at some posts, but I can not execute.

INSERT INTO 'pagina_extra' ('ID_PExtra', 'ordem_paginas', 'id_menu', 'url_seo', 'url', 'nome', 'titulo', 'conteudo', 'exibir', 'interno')
SELECT * FROM (SELECT 'NULL', '3', '1', 'downloads', 'pag_downloads.php', 'Downloads', '', '', 'N', 'N') AS tmp
WHERE NOT EXISTS (
    SELECT url_seo FROM pagina_extra WHERE url_seo = 'downloads'
) LIMIT 1;

Error that appears when I go directly to Phpmyadmin:

  

Duplicate column name 'Downloads'

But there is no value to accuse duplicate. What's wrong?

    
asked by anonymous 08.11.2015 / 20:40

2 answers

0

Test as follows:

DECLARE @Var VARCHAR(50)
SET @Var = (SELECT url_seo FROM pagina_extra WHERE url_seo = 'downloads')
IF @Var <> 'downloads'
BEGIN
    INSERT INTO pagina_extra (ID_PExtra, ordem_paginas, id_menu, url_seo, url, nome, titulo, conteudo, exibir, interno)
    SELECT * FROM (SELECT 'NULL', '3', '1', 'downloads', 'pag_downloads.php', 'Downloads', '', '', 'N', 'N') AS tmp
END
    
09.11.2015 / 16:06
0

You can use LOWER to make the value of your field identical.

INSERT INTO 'pagina_extra' ('ID_PExtra', 'ordem_paginas', 'id_menu', 'url_seo', 'url', 'nome', 'titulo', 'conteudo', 'exibir', 'interno')
SELECT * FROM (SELECT 'NULL', '3', '1', 'downloads', 'pag_downloads.php', 'Downloads', '', '', 'N', 'N') AS tmp
WHERE NOT EXISTS (
    SELECT url_seo FROM pagina_extra WHERE LOWER(url_seo) = LOWER('downloads')
) LIMIT 1;
    
23.10.2017 / 14:40