select in elements that are not in the array

Asked

Viewed 52 times

3

I have a PHP array with Ids:

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

And a table called account in MYSQL:

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

Wanted using PDO to give a select in the table account only in the names that NAY are in that array. The answer I hope would be:

antonio, marcos, Leonardo

It is possible?

1 answer

4


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

You can do it like this:

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

Upshot:

select ID, NOME from comta Where ID not in (1,3,6);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.