MySQL select records from one table if their id AND username do not appear in a second table -
i have 2 tables this:
logbook: +------------+-------------+------+-----+-------------------+----------------+ | field | type | null | key | default | | +------------+-------------+------+-----+-------------------+----------------+ | id | int(11) | no | pri | null | auto_increment | | date_added | timestamp | no | | current_timestamp | | | username | varchar(16) | no | | null | | | entry | longtext | no | mul | null | | +------------+-------------+------+-----+-------------------+----------------+
and
read_logbook: +------------+-------------+------+-----+---------+----------------+ | field | type | null | key | default | | +------------+-------------+------+-----+---------+----------------+ | id | int(11) | no | pri | null | auto_increment | | logbook_id | int(11) | no | | null | | | username | varchar(16) | no | | null | | +------------+-------------+------+-----+---------+----------------+
what i'd select logbook
, if logbook.id
, logbook.username
not appear in read_logbook.logbook_id
, read_logbook.username
, respectively.
i've experimented left-union-right queries, not in
queries, , keep getting either thousands more results expected, or no results @ all.
any thoughts?
edit - i'd run specific username... basically, if username jmd9qs
, i'd want results logbook
read_logbook.id
!= logbook.id
, read_logbook.username
!= jmd9qs
i hope that's clear enough...
edit - test data
the logbook:
mysql> select id, date_added, username logbook order id desc limit 10; +----+---------------------+-----------+ | id | date_added | username | +----+---------------------+-----------+ | 94 | 2013-09-03 14:54:25 | tluce | | 93 | 2013-09-03 13:12:02 | tluce | | 92 | 2013-09-03 11:42:14 | tluce | | 91 | 2013-09-03 08:28:20 | jmd9qs | | 90 | 2013-09-03 07:13:36 | jmd9qs | | 89 | 2013-09-03 07:05:19 | jmd9qs | | 88 | 2013-09-03 06:57:47 | jsawtelle | | 87 | 2013-09-03 06:15:42 | jsawtelle | | 86 | 2013-09-03 05:21:14 | jsawtelle | | 85 | 2013-09-03 03:52:25 | jsawtelle | +----+---------------------+-----------+
logbook entries have been "marked" read:
mysql> select logbook_id, username read_logbook group logbook_id desc limit 10; +------------+----------+ | logbook_id | username | +------------+----------+ | 94 | jmd9qs | | 93 | jmd9qs | | 92 | jmd9qs | | 91 | jmd9qs | | 90 | jmd9qs | | 89 | jmd9qs | | 88 | jmd9qs | | 87 | jmd9qs | | 86 | jmd9qs | | 85 | jmd9qs | +------------+----------+ 10 rows in set (0.00 sec)
so when run query jmd9qs
, nothing should come because in read_logbook
, username , logbook id show up.
clarification -
so in logbook, username person wrote logbook.entry
. in read_logbook
, username person read entry. if i'm logged in jmd9qs
, , try view logbook, since i've read no logbook.entry
's should come up. user hasn't read specific entry, entry show up.
select * logbook logbook.username not in (select read_logbook.username read_logbook read_logbook.username='jmd9qs') , logbook.id not in (select read_logbook.logbook_id read_logbook);
Comments
Post a Comment