排除表之间的记录

时间:2016-01-04 17:47:33

标签: mysql

看一下下表。

spawns

+----------+---+----+----+--+
| id_spawn | position | map |
+----------+---+----+----+--+
|        1 |        1 |   1 |
|        2 |        2 |   1 |
+----------+----------+-----+

games;

+---------+-----+
| id_game | map |
+---------+-----+
|       1 |   1 |
|       2 |   1 |
|       3 |   1 |
|       4 |   1 |
|       5 |   1 |
+---------+-----+

warriors

+---------+------+----------+
| id_join | game | position |
+---------+------+----------+
|       1 |    1 |        2 |
+---------+------+----------+

我正试图从position spawns获取warriors game给出特定select s.position from spawns s left join games g on g.map = s.map left join warriors w on w.game = g.id_game where s.position not in(w.position) and g.id_game = 1; 。我第一次尝试:

1
  

查询按预期返回game = 5

然而,对于games我期待2个可用职位,但收到空的结果。我假设数据库引擎没有找到warriorsWORD "lieb" "Gefahr" "Spagetti" "hallo" "danach" "schiebt" "ganzem" "lässt" "beginnen" "Schiff" ... 之间的关系,因为游戏没有比较战士。那么,我怎样才能获得这些职位呢?

2 个答案:

答案 0 :(得分:2)

如果我理解正确,对于给定的gameid,您希望返回positions表格中spawns的{​​{1}} positions warriors outer join 1}}表?如果是这样,这是使用null / select s.position from spawns s join games g on g.map = s.map left join warriors w on w.game = g.id_game and s.position = w.position where g.id_game = 1 and w.id_join is null 支票的一个选项。

not exists
BTW,另一种常见方法是使用mysql。虽然大多数数据库都可以优化这种方法,但var io = require('socket.io-client'); var $ = require('jquery'); var socket = io(); $('form').submit(function(){ socket.emit('chat message', $('#m').val()); $('#m').val(''); return false; }); socket.on('chat message', function(msg){ $('#messages').append($('<li>').text(msg)); }); 似乎有问题。以下是关于此事的一些好帖子:

答案 1 :(得分:1)

你可以使用NOT EXISTS来排除在给定特定游戏的战士中使用过的生成位置:

SELECT s.position
FROM spawns AS s
INNER JOIN games AS g ON g.map = s.map
WHERE g.id_game = ? AND 
      NOT EXISTS (SELECT 1
                  FROM warriors AS w
                  WHERE w.game = g.id_game AND w.position = s.position)