sql >> Base de Datos >  >> RDS >> Mysql

Insertar registro si no existe sin clave única

¿Estás buscando esto?

EDITAR:

 INSERT INTO `clicks` (
  `id` ,
  `file_id` ,
  `country` ,
  `date` ,
  `timeClick` ,
  `user_subid`
 )
 SELECT `id` ,
`file_id` ,
`country` ,
`date` ,
`timeClick` ,
`user_subid`
FROM  
 (SELECT NULL id , '1500' `file_id` , 'Canada' `country` , '2014-05-24' `date` , '1400934475' `timeClick` , '7c282b9227328778680d5826e01f6d' `user_subid`) a 
LEFT JOIN `clicks` b ON 
  a.id = b.id AND a.`file_id` = b.`file_id` AND a.`user_subid` = b.`user_subid` 
WHERE b.`user_subid` IS NULL;

o con NO EXISTE

  INSERT INTO `clicks` (
  `id` ,
  `file_id` ,
  `country` ,
  `date` ,
  `timeClick` ,
  `user_subid`
 )
 SELECT `id` ,
`file_id` ,
`country` ,
`date` ,
`timeClick` ,
`user_subid`
FROM  
 (SELECT NULL id , '1500' `file_id` , 'Canada' `country` , '2014-05-24' `date` , '1400934475' `timeClick` , '7c282b9227328778680d5826e01f6d' `user_subid`) a
 WHERE NOT EXISTS (
   SELECT 1 
   FROM `clicks` 
   WHERE `user_subid` = '7c282b9227328778680d5826e01f6d')