select on elements that are not in array [duplicate]

3

I have an array in PHP with IDs:

$idNaoEnvia['1', '3', '6']

And a table named account in MYSQL:

ID |  NOME
1  |  caio
2  |  antonio
3  |  cleber
4  |  marcos
5  |  leonardo
6  |  andre

I wanted to use PDO to give a select in the account table only on the NOT names in this array. The answer I hope would be:

antonio, marcos, leonardo

Is it possible?

    
asked by anonymous 23.08.2016 / 00:08

1 answer

4

Use NOT IN to filter and implode to convert array to string, separated by commas.

You can do this:

<?php
$idNaoEnvia = ['1', '3', '6'];
$sql = 'select ID, NOME from comta where ID not in (' . implode(',', $idNaoEnvia) . ');';
echo $sql;

Result:

  

select ID, NAME from comta where ID not in (1,3,6);

    
23.08.2016 / 00:15