Reputation: 93
I have the following table with all books:
And another table to record borrowed books:
My expected output should show the list of books with the current ammount (named jumlah_buku
), calculated by substracting borrowed books:
This is what I have tried:
SELECT BUKU.KODE_BUKU AS 'KODE BUKU',BUKU.JUDUL AS 'JUDUL',BUKU.NAMA_PENGARANG AS 'NAMA PENGARANG', (BUKU.JUMLAH_BUKU) - (SELECT COUNT(*) FROM (select KODE_BUKU from PEMINJAMAN)) AS 'JUMLAH BUKU' FROM BUKU INNER JOIN PEMINJAMAN ON BUKU.KODE_BUKU = PEMINJAMAN.KODE_BUKU
But it's not working.
Please help me.
Upvotes: 0
Views: 39
Reputation: 9618
Try this correlated subquery:
SELECT BUKU.KODE_BUKU AS 'KODE BUKU',
BUKU.JUDUL AS 'JUDUL',
BUKU.NAMA_PENGARANG AS 'NAMA PENGARANG',
BUKU.JUMLAH_BUKU - (
SELECT COUNT(KODE_BUKU)
FROM PEMINJAMAN
WHERE BUKU.KODE_BUKU = PEMINJAMAN.KODE_BUKU
) AS 'JUMLAH BUKU'
FROM BUKU;
Upvotes: 1