Hi I have a problem with selecting. I have an application, where admin can create multiple lists of books and those lists have users assigned to them and then they can choose what books, they want and they can print their selection. I have problem with displaying the list on the user part. A user can add or delete books from their selection. I think I'm missing some conditions.
Table users:
+----+-------+----------+
| id | name | class_id |
+----+-------+----------+
| 1 | John | 1 |
| 2 | Jenna | 2 |
+----+-------+----------+
Table classes
+-----+--------+
| id | name |
+-----+--------+
| 1 | class1 |
| 2 | class2 |
+-----+--------+
Table books:
+----+-------+
| id | name |
+----+-------+
| 1 | test1 |
| 2 | test2 |
| 3 | test3 |
| 4 | test4 |
| 5 | test5 |
+----+-------+
Table lists
+-----+----------+
| id | class_id |
+-----+----------+
| 1 | 1 |
| 2 | 2 |
+-----+----------+
Table records
+---------+---------+
| list_id | book_id |
+---------+---------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 4 |
| 2 | 5 |
+---------+---------+
Table selection
+---------+---------+---------+
| list_id | book_id | user_id |
+---------+---------+---------+
| 1 | 1 | 1 |
| 1 | 2 | 1 |
| 2 | 4 | 2 |
+---------+---------+---------+
I need to select the name of the books, which are not in the selection, but they are in the list
I've tried this, but it's not working, how it should:
SELECT b.id
, b.name
FROM records r
join books b
on b.id=r.book_id
join lists l
on l.id=t.list_id
join class c
on c.id=l.class_id
join users u
on u.class_id=c.id
left
join selection s
on r.book_id = s.book_id
where class_id=(select class_id from users where id=1)
and r.list_id=1
and not EXISTS (select book_id from selection)
r.list_id is switched by a session
So in the case of John I would like to see test3 and in the case of Jenna test1 and test5.
And I have a problem, that if I puttest1 into John's list, it will not be displayed in Jenna's.
答案 0 :(得分:0)
我尝试过不在
中的r.book_id SELECT b.id, b.name
FROM records r
JOIN books b on(b.id=r.book_id)
JOIN lists l on(l.id=t.list_id)
JOIN class c on(c.id=l.class_id)
JOIN users u on(u.class_id=c.id)
LEFT JOIN selection s on(r.book_id=s.book_id)
WHERE class_id =(SELECT id_tridy FROM studenti where id=1)
AND r.list_id=1 AND r.book_id NOT IN (select book_id from selection)
答案 1 :(得分:0)
SELECT b.id
, b.name
FROM records r
join books b
on b.id=r.book_id
join lists l
on l.id=t.list_id
join class c
on c.id=l.class_id
join users u
on u.class_id=c.id
left
join selection s
on r.book_id = s.book_id
where l.id=(select l.id from lists join classes c on(l.class_id=c.id) join users u on(u.class_id=c.id) where u.id=1) and r.book_id not in(select book_id from selection where user_id=1)