0

我有两个这样的表:

logbook:
+------------+-------------+------+-----+-------------------+----------------+
| Field      | Type        | Null | Key | Default           | Extra          |
+------------+-------------+------+-----+-------------------+----------------+
| id         | int(11)     | NO   | PRI | NULL              | auto_increment |
| date_added | timestamp   | NO   |     | CURRENT_TIMESTAMP |                |
| username   | varchar(16) | NO   |     | NULL              |                |
| entry      | longtext    | NO   | MUL | NULL              |                |
+------------+-------------+------+-----+-------------------+----------------+

read_logbook:
+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| id         | int(11)     | NO   | PRI | NULL    | auto_increment |
| logbook_id | int(11)     | NO   |     | NULL    |                |
| username   | varchar(16) | NO   |     | NULL    |                |
+------------+-------------+------+-----+---------+----------------+

我想做的是从 中选择所有内容logbook,但前提是logbook.idANDlogbook.username不会分别出现在read_logbook.logbook_idread_logbook.username中。

我已经尝试了一些左联合右查询以及一些not in查询,并且不断得到比预期多出数千个结果,或者根本没有结果。

有什么想法吗?

编辑 - 我会为一个特定的用户名运行它......所以基本上,如果我的用户名是jmd9qs,我想要所有结果来自logbookwhere read_logbook.id!=logbook.idread_logbook.username!=jmd9qs

我希望这足够清楚...

编辑 - 测试数据

日志:

mysql> select id, date_added, username from logbook order by 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 |
+----+---------------------+-----------+

已“标记”为已读的日志条目:

mysql> select logbook_id, username from read_logbook group by 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)

因此,当我对 运行查询时jmd9qs,什么都不会出现,因为在 中read_logbook,他的用户名和日志 ID 出现了。

澄清 -

所以在日志中,用户名就是写的人logbook.entry。在read_logbook中,用户名是阅读该条目的人。因此,如果我以 身份登录jmd9qs,并且我尝试查看日志,因为我已经阅读了所有不logbook.entry应该出现的内容。但是对于尚未阅读该特定条目的另一个用户,该条目会出现。

4

2 回答 2

1

如果我了解您的需求,您应该尝试

SELECT t1.* FROM logbook t1
LEFT JOIN read_logbook t2
  ON t1.id = t2.id AND t1.username = t2.username
WHERE t2.id IS NULL AND t2.username IS NULL
于 2013-09-04T06:07:51.880 回答
1
SELECT * 
FROM   logbook 
WHERE  logbook.username NOT IN 
    (SELECT read_logbook.username 
     FROM   read_logbook 
     WHERE  read_logbook.username='jmd9qs')
AND logbook.id NOT IN
    (SELECT read_logbook.logbook_id
     FROM   read_logbook);
于 2013-09-04T06:21:36.070 回答