【问题标题】:Join two tables to one - Sql将两个表连接到一个 - Sql
【发布时间】:2014-05-28 13:38:57
【问题描述】:
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 中的所有行,还显示了表scripts 中的连接列script 和表storage_cost 中的列npc_data if 他们@987654327 @列的值匹配scripts的objectid和npc_data的npcid。

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

谢谢

【问题讨论】:

  • 您使用的是什么数据库,SQL Server 还是 MySQL?更重要的是,我在select 列表中没有看到storage_cost
  • 您能否详细说明您的问题。

标签: mysql sql sql-server


【解决方案1】:

您的查询在返回的数据集中缺少[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] 表中,那么您可以通过将WHERE 子句中的[helper] = 0 更改为来修改上面的查询script.[helper] = 0.

添加:

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

我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 2012-02-28
    • 2012-05-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 2011-12-27
    相关资源
    最近更新 更多