简体   繁体   English

使用可能的空记录查询 SQL 值

[英]Query SQL values with possible empty records

I have 4 tables that I want to run a single query on.我有 4 个要运行单个查询的表。 I want specific values from each table that all relate to a single user.我希望每个表中的特定值都与单个用户相关。

However, some of these tables may not have any associated records.但是,其中一些表可能没有任何关联的记录。 If there are no records in a given table, the query set comes back as empty (because there are no records pertaining to the specified user in the table).如果给定表中没有记录,则查询集返回为空(因为表中没有与指定用户相关的记录)。

Question问题

How do we pull all values for a user and insert blanks/null in the columns that have no values?我们如何为用户提取所有值并在没有值的列中插入空白/空值?

My query:我的查询:

SELECT PLAYER.UUID,
       XP,
       RANKS.RANKNAME,
       HORSE.HEALTH_LEVEL,
       HORSE.SPEED_LEVEL,
       HORSE.JUMP_LEVEL,
       special_weapons.name
FROM PLAYER,
     HORSE,
     RANKS,
     Special_weapons
WHERE PLAYER.UUID = HORSE.UUID
  AND PLAYER.RANKID = RANKS.RANKID
  AND PLAYER.UUID = 'sldjnofw-adfdafd-113rsada'
  AND Player.uuid = special_weapons.uuid;

My table structure:我的表结构:

在此处输入图像描述

My table contents:我的表格内容:

在此处输入图像描述

The special weapons table in this case is empty.在这种情况下,特殊武器表是空的。 Blanks should populate missing columns in the above query.空白填充上述查询中的缺失列。 Instead, an empty set is returned.而是返回一个空集。

You need LEFT JOIN in such cases.在这种情况下,您需要 LEFT JOIN。 Try below试试下面

SELECT player.uuid
      ,xp
      ,ranks.rankname
      ,horse.health_level
      ,horse.speed_level
      ,horse.jump_level
      ,special_weapons.name
FROM   player
LEFT   JOIN horse
ON     player.uuid = horse.uuid
LEFT   JOIN ranks
ON     player.rankid = ranks.rankid
LEFT   JOIN special_weapons
ON     player.uuid = special_weapons.uuid
WHERE  player.uuid = 'sldjnofw-adfdafd-113rsada';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM