<?php
//$g=$mysqli->query("SELECT * FROM books where id in (SELECT book_id from author_books)"); есть автора
$g=$mysqli->query("SELECT * FROM books where id not in (SELECT book_id from author_books)");
$result=[];
while($b=mysqli_fetch_assoc($g)){
$result[]=$b;
}
?>
<h4>Книги без автора</h4>
<table border="1">
<tr>
<th>Книгa</th>
</tr>
<?php
foreach($result as $v){
echo '<tr>
<td>'.$v['b_name'].'</td>
</tr> ';
}
?>
</table>
<hr>
<?php
$g=$mysqli->query("SELECT * FROM books where id not in (SELECT book_id from author_books)");
$result=[];
while($b=mysqli_fetch_assoc($g)){
$result[]=$b;
}
?>
<h4>Авторы у которых более одной книги</h4>
<table border="1">
<tr>
<th>Автора</th>
</tr>
<?php
foreach($result as $v){
echo '<tr>
<td>'.$v['b_name'].'</td>
</tr> ';
}
?>
</table>
<hr>
SELECT a.*, b.count_book
FROM authors a,
(SELECT author_id, COUNT(book_id) as count_book
FROM author_books GROUP BY author_id
HAVING COUNT(book_id) > 1) b
WHERE a.id = b.author_id
$sql = "SELECT a.*, b.count_book FROM authors a, (SELECT author_id, COUNT(book_id) as count_book FROM author_books GROUP BY author_id HAVING COUNT(book_id) > 1) b WHERE a.id = b.author_id";
$results = $mysqli->query($sql);
foreach($results as $result) {
echo $result['f_name'] . " " . $result['s_name'] . " - " . $result['count_book'] . " книг";
}