Hi all
i have table with index on three column to avoid some duplicate cases
now i want to insert data to in and ignore insert if record with index already exist

may any one can help me ?!

Comments

manish.2184’s picture

alter table customers add unique index(user, email, address);

orkideh’s picture

Dear manish
i did indexing
my table already has index
know how to ingore this index in inserting in duplicate cases

manish.2184’s picture

index is set but you must set unique index. that;s why i give you a simple solution which alter table and set unique index.

orkideh’s picture

i did,
it prevent from inserting duplicated row, but i show error in each event, i want to solve that without error happening,

i mean each time script faced with duplicate row it prevent from inserting and continue application flow

Jaypan’s picture

This will only work if you are using mysql:

db_query(
  'INSERT INTO {some_table} ' .
  '(col1, col2) ' .
  'VALUES (:val1, :val2) ' .
  'ON DUPLICATE KEY UPDATE col1 = :val1 ',
  array
  (
    ':val1' => 'some_value',
    ':val2' => 'some_other_value',
  )
);

Note that col1, or a combination of col1 and col2, must be the primary key for {some_table}.

The Drupal organization has shut down discussion on improvement of the forums: https://www.drupal.org/node/2536122

It's time to start a new forum somewhere else. The Drupal organization does not care about the forums.

orkideh’s picture

thats nice,

is it way to implement this with some thing like

  db_insert('some_table')->addExpression(' ON DUPLICATE key Update  ');

?!

Jaypan’s picture

You can implement it exactly as I showed above.

The Drupal organization has shut down discussion on improvement of the forums: https://www.drupal.org/node/2536122

It's time to start a new forum somewhere else. The Drupal organization does not care about the forums.

orkideh’s picture

nice tip Jaypan thanks