Php 从与另一个表链接的books表中获取数据
我已经创建了图书网站,我有以下表格:Php 从与另一个表链接的books表中获取数据,php,mysql,Php,Mysql,我已经创建了图书网站,我有以下表格: 书籍(此表中有作者id) 作者\u compile\u rel(这是这些书的编撰者id) 现在我想,如果有人打开作者,假设(艾哈迈德·拉扎),那么我想展示这位作者的书,如果他写了这些书,甚至如果他编译了这些书 已编译的书籍和作者id输入到authors\u compile\u rel表中 我已经创建了下面的查询,但它没有显示作者编辑的书籍 $auth_id = mysql_real_escape_string($_GET['id']); $query
$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";
书和作者之间一定有关系,可能有书的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
请添加DDL以创建表,并使用参数化查询而不是字符串粘贴。强烈建议您阅读有关SQL注入的内容: