I have X tables and I want a single query to get all data which I want. The reason I want this method is I have more than 10.000 records in my database, so I must use only 1 query, because if I use 3-4 query and ten gether the data, it would take really long time to be executed by the server.
So the tables:
** book_info **
bookId status title
------ ------ -----
101 available How to cook
102 available IT tips and tricks
103 unavailable How to use your calc
etc...
** book_writers **
bookId writer
------ ------
101 Tom
102 Mike
103 Mike
etc...
** book_log **
bookId action client date
------ ------ ------ ----
103 loan Fred 2017-6-10
102 loan Anna 2017-6-8
101 return Anita 2017-5-3
101 loan Anita 2017-2-2
etc...
And what I want in only one query:
bookId status title writer lastLoan lastReturn
------ ------ ----- ------ -------- ----------
101 available How to cook Tom 2017-2-2 2017-5-3
102 available IT tips and tricks Mike 2017-6-8 -
etc...
I tried it by using JOIN and UNION, but I could not do it.
UPDATE
So since I got good solutions from you guys, I finally built the query that I have looked for. But one thing that I can not do is using "parent" query variable in "child" join query.
So I am having a table which contains the content of the books:
** book_content **
bookId content
------ -------
101 Lorem ipsum dolor sit amet, consectetur...
102 Donec ut lacus non odio blandit molestie
103 Interdum et malesuada fames ac ante ipsum...
etc...
I tried to query it like
select
bi.bookId
bd.content
from
books_info as bi
left outer join
(
select
bookId
content
from
book_content
where
bookId = bi.bookId
) bd on bi.bookId = bd.bookId
But the SQL says #1054 - Unknown column 'bi.bookId' in 'where clause'
in the join select. How can I use bi.bookId
in child join query?