0

i have created books site and i have tables below:

  1. Books (author id is there in this table)
  2. authors_compile_rel (here is the id of compilers of the books)

now i want that if someone opens the author suppose (ahmed raza) so i want to display the books of this author if he wrote the books or even if he compiled the books.

compiled books and the author id is entered in authors_compile_rel table.

i have created the query below but it is not showing the compiled books by the author.

$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";
  • Please add the DDL to create the tables, and also use parameterized queries instead of string-pasting. – o11c Jun 26 '15 at 00:01
  • Strongly suggest you to read about SQL injection: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Fabiano Araujo Jun 26 '15 at 00:49

1 Answers1

0

There must be a relation between books and authors_compile_rel tables, a book_id maybe. I assume there is this field as foreign key :

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
bignick
  • 121
  • 3