How to use NOT IN with data from another server in PHP

Asked

Viewed 48 times

1

I set up a table with Data that nay should be sought, I’ll call you here Table. The data that was in this table are these:

|Tabela_Not|
  Londres
  Tóquio
  Paris

And I applied this Query to make the searches discarding the items from Table:

SELECT * FROM BancoANoServidor1.registros WHERE Papel NOT IN (SELECT Papel FROM BancoANoServidor1.Tabela_Not);

As you can see in Query, the two tables are in the same database/server, my question is, the table records where I would like to do this search is on another Server (Bancobnoservier2.records).

Is there a way to solve this using only Mysql? Or is there another way to solve it in PHP? There is a way to do something like:

$Query_not = "SELECT Papel FROM BancoANoServidor1.Tabela_Not";
$result_not = mysqli_query($conecta1, $Query_not);

$Query_return = "SELECT * FROM BancoBNoServidor2.registros WHERE Papel NOT IN ($result_not)";
$return = mysqli_query($conecta2, $Query_return);

1 answer

1


You can loop in the first query.

The logic would be something like this:

while $result_not 
$negar = $negar + $result_not["Papel"] + ","

At the end of the loop you will have in the variable $deny something like: "London, Tokyo, Paris"

Oh yes you can use:

$Query_return = "SELECT * FROM BancoBNoServidor2.registros WHERE Papel NOT IN ($negar)";
  • You’ll make a mistake because you’ll get a comma at the end not?

  • @Shinchila_matadora this code does not work, but can be recycled logic.

  • $deny = substr($deny , 0, strlen(deny)-1);

  • @Claudioscarpafilho, I will try to implement here, thank you very much

Browser other questions tagged

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