You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

非规范化数据库中通过字段值转换实现跨表连接的方法咨询

How to Join Tables Using Direction Full Names and Abbreviations

Absolutely! Using CASE WHEN is a solid way to bridge those direction full names and abbreviations for your table join. Let’s break down the options you have:

Option 1: Direct CASE WHEN in the Join Clause

This works great if you only have a small set of directions to map. You’ll define the mapping directly in the ON condition of your join:

SELECT *
FROM your_full_name_table a
JOIN your_abbreviation_table b ON 
  CASE a.direction_or
    WHEN 'Northwest' THEN 'NW'
    WHEN 'Southwest' THEN 'SW'
    WHEN 'Northeast' THEN 'NE'
    WHEN 'Southeast' THEN 'SE'
    WHEN 'North' THEN 'N'
    WHEN 'South' THEN 'S'
    WHEN 'East' THEN 'E'
    WHEN 'West' THEN 'W'
    -- Add any other direction mappings you need here
  END = b.direction_or;

Pro tip: If there’s any chance of inconsistent capitalization (like "northwest" vs "Northwest"), normalize the text first using LOWER() or UPPER() to avoid mismatches:

CASE LOWER(a.direction_or)
  WHEN 'northwest' THEN 'NW'
  WHEN 'southwest' THEN 'SW'
  -- ... rest of the mappings
END = b.direction_or

Option 2: Use a Mapping CTE or Table (Better for Scalability)

If you have a lot of directions to map, or think you might need to update the mappings later, a common table expression (CTE) or permanent mapping table is cleaner and more maintainable:

Using a CTE:

WITH direction_mappings AS (
  SELECT 'Northwest' AS full_direction, 'NW' AS abbreviation UNION ALL
  SELECT 'Southwest', 'SW' UNION ALL
  SELECT 'Northeast', 'NE' UNION ALL
  SELECT 'Southeast', 'SE' UNION ALL
  SELECT 'North', 'N' UNION ALL
  SELECT 'South', 'S' UNION ALL
  SELECT 'East', 'E' UNION ALL
  SELECT 'West', 'W'
)
SELECT *
FROM your_full_name_table a
JOIN direction_mappings dm ON a.direction_or = dm.full_direction
JOIN your_abbreviation_table b ON dm.abbreviation = b.direction_or;

Permanent Mapping Table (Best for Large Datasets):

If your tables are large, creating a permanent mapping table with indexes will boost join performance:

-- First create the mapping table
CREATE TABLE direction_mapping (
  full_direction VARCHAR(50) PRIMARY KEY,
  abbreviation VARCHAR(10) NOT NULL UNIQUE
);

-- Insert your mappings
INSERT INTO direction_mapping (full_direction, abbreviation)
VALUES
  ('Northwest', 'NW'),
  ('Southwest', 'SW'),
  ('Northeast', 'NE'),
  ('Southeast', 'SE'),
  ('North', 'N'),
  ('South', 'S'),
  ('East', 'E'),
  ('West', 'W');

-- Then join using the mapping table
SELECT *
FROM your_full_name_table a
JOIN direction_mapping dm ON a.direction_or = dm.full_direction
JOIN your_abbreviation_table b ON dm.abbreviation = b.direction_or;

This approach keeps your main query clean, makes it easy to update mappings later, and the indexes on the mapping table will speed up the join operation.

内容的提问来源于stack exchange,提问作者Andrew Brown

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:49:34