Y
Y
Yura_Mart2018-05-26 18:18:26
PHP
Yura_Mart, 2018-05-26 18:18:26

Query output comparing 2 tables. If a file from one table matches the id of a file from the second table, then display everything else except for it?

There are 2 tables:
admin_files:

id  |  signature
 1   |        1
 2   |        1
 3   |        1

support_files:
id  |  support_id  |  admin_file_id
 1   |        2           |        1  
 2   |        2           |        2 
 3   |        2           |        3

function signature($id){
    global $connection;
    $query = "SELECT COUNT(*) as kol FROM admin_files WHERE signature = 1 ";
    $res = mysqli_query($connection, $query);
    $row = mysqli_fetch_assoc($res);
    $kol = $row["kol"];
    $query = "SELECT a.* FROM admin_files a JOIN support_files s ON s.admin_file_id = a.id WHERE a.signature= 1 AND s.support_id='$id'";
$res = mysqli_query($connection, $query);
if(mysqli_num_rows($res) == $kol){
    return;
}else{
    $query = "SELECT a.* FROM admin_files a JOIN support_files s ON s.admin_file_id != a.id WHERE a.signature= 1 AND s.support_id='$id'";
    $res = mysqli_query($connection, $query);
    if(mysqli_num_rows($res) > 0){
        $signature = array();
        while($row = mysqli_fetch_assoc($res)){
            $signature[$row['id']] = $row;
        }
        return $signature;
    }else{
        $query = "SELECT * FROM admin_files WHERE signature = 1 ORDER BY id DESC ";
        $res = mysqli_query($connection, $query);
        $signature = array();
        while($row = mysqli_fetch_assoc($res)){
            $signature[$row['id']] = $row;
        }
        return $signature;
    }
}
}

You need to output files from the admin_files table that are not in the support_files table, otherwise output all files from the admin_files table. And if all the files that are in the admin_files table are also in the support_files table, then output nothing. It outputs normally until 2 files from both tables match. As soon as 2 files match, it starts displaying everything that is in the admin_files table. Tell me, please, what is the error?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yura_Mart, 2018-05-27
@Yura_Mart

From Vladislav's hint, here's what happened, it works as intended.

function signature($id){
    global $connection;
    $query = "SELECT a.* FROM admin_files a LEFT JOIN support_files s ON s.admin_file_id = a.id AND s.support_id='$id' WHERE s.admin_file_id IS NULL AND a.signature= 1";
    $res = mysqli_query($connection, $query);
        $signature = array();
        while($row = mysqli_fetch_assoc($res)){
            $signature[$row['id']] = $row;
        }
        return $signature;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question