9

I have a table "groupdentlink" where I want to delete all the rows that weren't checked in a form.

In essence I want to perform a query like:

DELETE * FROM groupdentlink
WHERE group_id = 'a'
AND dentist_id IS NOT IN ARRAY 'b'

I think I could set a variable with a foreach loop and then keep adding the array values to it so I end up with:

DELETE * FROM groupdentlink
WHERE group_id = 'a'
AND dentist_id != 'D1'
AND dentist_id != 'D5'
AND dentist_id != 'D8'

...and so on.

But is this really the right/best way to do this?

Thanks in advance!

1
  • Wait, even that solution is throwing a syntax error... hmmm. Commented Nov 2, 2011 at 18:07

7 Answers 7

20
DELETE FROM groupdentlink
WHERE group_id = 'a'
AND dentist_id NOT IN ('D1','D5','D8')

More info here http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_not-in

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

2 Comments

Thanks for the help. I know I'm close now, but getting syntax error with this query: DELETE * FROM groupdentlink WHERE group_id = 'a' AND dentist_id NOT IN ('UTG1','UTG10','UTG100')
DELETE * FROM -> DELETE FROM. I didn't saw this as well :)
2

If you want to execute this query from a Zend Framework driven application please take in consideration the followings :

$where = sprintf('dentist_id NOT IN ("%s")', implode('", "',array_map('mysql_escape_string', $array))); 
$this->sqlMapper->delete($where);

If you try . operator for concatenation purposes the query will result in a fatal error because of the quotes. So from my experience using htmlspecialchars or htmlencode along with . operator will only consume your time and patience. The use of sprintf is elegant, helps you keep your code clean.

And I think these observations apply to any application that makes use of php objects.

Comments

1

New user to Stack Exchange, please forgive and instruct if I'm committing a faux pas.

The preceeding answer is incredibly dangerous, because it opens you up to SQL injection attacks.

Always use bind params.

Always use bind params.

Always use bind params.

Hint: if your query does not resemble "DELETE * FROM groupdentlink WHERE group_id = 'a' AND dentist_id IS NOT IN (?, ?, ?);" you are doing it wrong.

Comments

1

An elegant, fully parametrized solution (using PDO):

$dentistIds = ['D1', 'D5', 'D8'];

$query = sprintf(
    "DELETE FROM online_order_shipping
           WHERE group_id = 'a'
             AND dentist_id NOT IN (%s)",
    implode(',', array_fill(0, count($dentistIds), '?'))
);

$stmtDelete = $pdo->prepare($query);
$stmtDelete->execute($dentistIds);

The implode function strings ? together with , without adding a comma in the end (source). You could turn that into a function to make it more readable, otherwise the sprintf keeps it nice and tidy without ugly string concatenation.

Comments

0

I found the statement $str = rtrim($str, ",");
didnt remove the trailing comma giving rise to an error
I came up with this work around:

// string without quotes

$str = array_shift($array);

// string with quotes

$str = "'" . array_shift($array) . "'";



foreach ($array as $item)
{

    $str .= ", '" . $item . "'";

}

1 Comment

Does this really answer the question?
0

Here How I do it :

assuming you have an array called $csvstocknumbers = ['0','1']; $stocknumbers = implode(",",$csvstocknumbers) ; // make the content of the array in one string seprated by , $deleteoldvehicles = "DELETE FROM table_name WHERE column_name NOT IN
($stocknumbers ) ;"; mysqli_query($con, $deleteoldvehicles);

Comments

-1

You just need to join the ids with a comma:

$myarray = new array('D1', 'D5', 'D8');
$str = "";
foreach ($myarray as $item)
{
    $str .= $item . ",";
}
$str = rtrim($str, ",");

$query = "DELETE * FROM groupdentlink
             WHERE group_id = 'a'
             AND dentist_id NOT IN ($str)";

This will give you a query like this:

DELETE * FROM groupdentlink
    WHERE group_id = 'a'
    AND dentist_id IS NOT IN (D1, D5, D8);

If you need the quotes around the ids, then change the loop like this:

foreach ($myarray as $item)
{
    $str .= "'".$item . "',";
}

1 Comment

Darnit. Thanks for the help. I'm getting syntax errors with this query: DELETE * FROM groupdentlink WHERE group_id = 'a' AND dentist_id IS NOT IN ('UTG1','UTG10','UTG100')

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.