Select different values in two tables - SQL and PHP

0

I have two tables in the tt_order and the request_grade , what I want to do is compare the values in the " request " column of the table and the column " n_pedido " of the request_grade, basically I would like to get the values you have in the tb_order and do not have in the_grade_order.

I tried the following code:

$sql = "SELECT pedido FROM pedido_tb WHERE pedido NOT IN (SELECT n_pedido FROM pedido_grade)";

$disp_sql = $mysqli->query($sql);

$num = $disp_sql->num_rows;

echo "</br>número de linhas ".$num."</br>";

Does anyone have any idea why it does not return anything?

    
asked by anonymous 22.11.2017 / 17:02

3 answers

2

Just do a simple SELECT with subquery

SELECT pedido FROM table1 WHERE pedido NOT IN (SELECT n_pedido FROM table2)

However, if you need one day to display records of the two tables you can use JOIN , so I'll leave you an example for you to study

INNER JOIN

SELECT pedido.table, n_pedido.table2 FROM table1
    INNER JOIN table2 ON table1.id = table2.id

LEFT OUTER JOIN

SELECT pedido.table, n_pedido.table2 FROM table1
     LEFT OUTER JOIN table2 ON table1.id = table2.id
    
22.11.2017 / 17:13
2
SELECT pedido FROM table1 WHERE pedido NOT IN (SELECT n_pedido FROM table2)
    
22.11.2017 / 17:07
0

Make a query with LEFT JOIN and do 1 WHERE where the column of the left table is null, then returns only the requests that do not have a grid request:

SELECT
    p.*
FROM pedido p
LEFT JOIN pedido_grade pg
ON p.pedido = pg.n_pedido
WHERE pg.n_pedido IS NULL;
    
24.11.2017 / 21:35