从与另一个表链接的books表中获取数据


get data from books table linked with another table

我已经创建了图书网站,下面有表格:

  1. 书籍(作者id在这张表中)
  2. authors_compile_rel(这是这些书的编撰者的id)

现在我想,如果有人打开作者假设(艾哈迈德·拉扎),所以我想展示这个作者的书,如果他写了书,甚至如果他编辑了书。

已编译的书籍,作者id输入authors-compile_rel表中。

我已经创建了下面的查询,但它没有显示作者编辑的书籍。

$auth_id    = mysql_real_escape_string($_GET['id']);
$query      = "
SELECT b.id, b.unique_name, b.native_name, b.auth_id, b.status, b.create_date, ar.auth_id FROM books b, authors_compile_rel ar
WHERE b.status = 1 AND b.auth_id = ".$auth_id." OR b.t_auth_id = ".$auth_id."
OR b.id = ".$auth_id." OR ar.auth_id = ".$auth_id."
ORDER by id DESC";

books和authors_compile_rel表之间必须存在关系,可能是book_id。我假设有这个字段作为外键:

SELECT 
b.id, b.unique_name, b.native_name, b.auth_id, b.status, b.create_date, ar.auth_id 
FROM books b LEFT OUTER JOIN authors_compile_rel ar ON (b.id = ar.book_id)
WHERE 
b.status = 1 AND ( b.auth_id = ".$auth_id." OR b.t_auth_id = ".$auth_id." OR ar.auth_id = ".$auth_id." )
ORDER by b.id DESC