2015-06-25 21 views
0

聯繫我創建圖書的網站,我有如下表:獲得從書本上表中的數據與另一個表

  1. 圖書(作者ID是否有此表)
  2. authors_compile_rel(這裏是編譯器的ID的書)

現在我想如果有人打開作者假設(艾哈邁德raza),所以我想顯示這位作者的書,如果他寫的書,或者,即使他編譯的書。

已編譯的書籍和author_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"; 
+0

請添加DDL來創建表,並且還使用參數化查詢而不是字符串粘貼。 – o11c

+0

強烈建議您閱讀有關SQL注入:http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php –

回答

0

書和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 
相關問題