0

我目前并成功地在 TABLE A + B 之间进行了 OUTER JOIN,其中包含以下内容:

SELECT * FROM player_img
  LEFT OUTER JOIN user_play
  ON player_img.player_img_id = user_play.user_play_entry_player_img_id
  AND user_play.user_play_uid = 1
  WHERE player_img.player_img_id IS null

这给了我来自表 A 的所有结果,这些结果没有来自表 B 的任何参考user_play_uid = 1

在表 A 中,我有引用其他表的属性——我也想查询这些字段。一些表引用的快速示例:

TABLE_A:
player_img_id (unique key)
player_img_player_id (reference to: TABLE_C "player_id")
player_img_category_id (reference to: TABLE_D "category_id")

TABLE_B:
user_play_entry_player_img_id (reference to TABLE_A: "player_img_id")
user_play_uid (reference to table with user.id)

TABLE_C:
player_id (unique key)
player_country (reference to table with country.id)
player_league (reference to table with league.id)

TABLE_D:
category_id (unique key)

认为我可以按照以下方式做一些事情(这当然根本不起作用)

SELECT u . * , up . * , pi . * , p . * , c . *
  FROM user u, user_play up, player_img pi, player p, country c
  LEFT OUTER JOIN up
  ON pi.player_img_id = up.user_play_entry_player_img_id
  WHERE pi.player_img_id IS null
  AND up.user_play_uid = $this->user_id
  AND pi.player_img_category_id = $this->category_id
  AND pi.player_img_player_id = p.player_id
  AND p.player_country = c.country_id

关于如何将我的 OUTER JOIN 与参考查找表结合起来的任何建议?

编辑:当前尝试与以下建议 - 查询正在运行但没有显示结果 - 想法?:/

SELECT u . * , up . * , pi . * , p . * , c . *
  FROM user u
  INNER JOIN player_img pi
  ON pi.player_img_category_id  = 3
  INNER JOIN player p
  ON pi.player_img_player_id    = p.player_id
  INNER JOIN country c
  ON p.player_country           = c.country_id
  LEFT OUTER JOIN user_play up
  ON pi.player_img_id = up.user_play_entry_player_img_id
  WHERE pi.player_img_id IS null
  AND up.user_play_uid      != 1
4

1 回答 1

0

使用 ANSI-92JOIN语法而不是您正在使用的旧语法:

SELECT u . * , up . * , pi . * , p . * , c . *
FROM user u
INNER JOIN player_img     pi ON ...
INNER JOIN player          p ON pi.player_img_player_id = p.player_id
INNER JOIN country         c ON p.player_country        = c.country_id
LEFT OUTER JOIN user_play up ON pi.player_img_id    = up.user_play_entry_player_img_id
WHERE pi.player_img_id IS null
  AND up.user_play_uid = $this->user_id

注意事项:

  • 您的查询中没有关于如何JOIN将表player_img pi与其他表一起使用的条件。

  • 该条件AND up.user_play_uid = $this->user_id可能会删除未捕获的行,因为LEFT JOIN在这种情况下,您可能需要将该条件移动到子查询中,而不是LEFT OUTER JOIN直接移动到该表中。

于 2013-09-04T09:59:30.370 回答