将两个表连接到一个 - Sql

时间:2014-05-28 13:38:57

标签: mysql sql sql-server

SELECT 
id, mapid, life_type, lifeid, x_pos, y_pos, foothold, min_click_pos, max_click_pos, respawn_time, life.flags, script.script 
FROM 
map_life life 
LEFT JOIN 
scripts script 
ON 
script.objectid = life.lifeid 
AND 
script.script_type = 'npc' 
AND 
helper = 0 
LEFT JOIN 
npc_data n 
ON 
n.npcid = life.lifeid 
AND script.script_type = 'npc'

我试图执行以下脚本。基本上,我会显示表格map_life中的所有行,并且还会从表格script和表格scripts中加入列storage_cost。 } 如果他们npc_data列的值匹配lifeid' s objectid和scripts的npcid。

但是,它无法正常工作。为什么?我无法看到storage_cost的正确值。

由于

1 个答案:

答案 0 :(得分:0)

您的查询缺少返回数据集中的[storage_cost]列:

SELECT 
    life.[id], 
    life.[mapid], 
    life.[life_type], 
    life.[lifeid], 
    life.[x_pos], 
    life.[y_pos], 
    life.[foothold], 
    life.[min_click_pos], 
    life.[max_click_pos], 
    life.[respawn_time], 
    life.[flags], 
    script.[script], 
    npc.[storage_cost] 
FROM 
    [map_life] AS life 
    LEFT OUTER JOIN [scripts] AS script 
        ON ( life.[lifeid] = script.[objectid] ) 
    LEFT OUTER JOIN [npc_data] AS npc 
        ON ( life.[lifeid] = npc.[npcid] )
WHERE 
    script.[script_type] = 'npc' 
    AND [helper] = 0

此外,还不清楚[helper]列中存在哪个表。如果该列位于[scripts]表中,则可以通过更改[helper] = 0 in来修改上述查询WHERE子句改为script.[helper] = 0

<子>的加了:

当然,由于您使用的是LEFT OUTER JOIN,因此script.[script]npc.[storage_cost]的返回值可能是 NULL

我希望这会有所帮助。