表1flow_paypertime_view 字段contract_code, contract_name,specialty_code,specialty_name,end_date,send_date,sheet_state,actual_free,actual_works,county_code,region_code 表2gz_dim_area_mapping 字段 dim2_code,area_code 现在要查询表一中 county_code或者region_code等于表2area_code的数据对应的dim2_code,结果字段为contract_code, contract_name,specialty_code,specialty_name,end_date,send_date,sheet_state,actual_free,actual_works,region_code 如果region_code等于dim2_code,则结果字段为cityCode,如果county_code等于dim2_code,则结果字段显示为countryCode
时间: 2023-12-11 16:05:12 浏览: 133
您可以使用以下 SQL 查询语句来实现您的需求:
```sql
SELECT
t1.contract_code,
t1.contract_name,
t1.specialty_code,
t1.specialty_name,
t1.end_date,
t1.send_date,
t1.sheet_state,
t1.actual_free,
t1.actual_works,
CASE
WHEN t1.region_code = t2.dim2_code THEN t2.area_code
ELSE NULL
END AS cityCode,
CASE
WHEN t1.county_code = t2.dim2_code THEN t2.area_code
ELSE NULL
END AS countryCode
FROM
flow_paypertime_view AS t1
LEFT JOIN
gz_dim_area_mapping AS t2 ON (t1.county_code = t2.area_code OR t1.region_code = t2.area_code)
```
这个查询语句使用了 LEFT JOIN 来连接两个表,并通过 CASE WHEN 语句来判断条件。如果 region_code 等于 dim2_code,则结果字段为 cityCode,如果 county_code 等于 dim2_code,则结果字段为 countryCode。
阅读全文