简体   繁体   English

Map 结果从一个表到另一个查询中的不同列

[英]Map results from one table to different columns in another query

I'm trying to retrieve a set of records from table that has descriptions on another table in same database.我正在尝试从表中检索一组记录,该表在同一数据库中的另一个表上有描述。

  1. Table 1:表格1:
    ColA1 | ColA1 | DescrpA1描述A1
    -- A - - - - Apple - 一个苹果
    -- B - - - - Orange -- B - - - - 橙色
    -- C - - - - Banana -- C - - - - 香蕉
    -- D - - - - Watermelon -- D - - - - 西瓜

  2. Table 2:表 2:
    ColA2 | ColA2 | Qty1 |数量1 | ColB2 | ColB2 | Qty2 |数量2 |
    --A - - - - - 1 - - - C - - - - 1 --A - - - - - 1 - - - C - - - - 1
    --C - - - - - 1 - - - D - - - - 2 --C - - - - - 1 - - - D - - - - 2
    --B - - - - - 1 - - - A - - - - 1 --B - - - - - 1 - - - A - - - - 1

What I want is a way to map descriptions from Table1 into results of Table2我想要的是一种将表1中的map描述转换为表2结果的方法

  1. Result:结果:
    ColA2 | ColA2 | Decription |说明 | Qty1 |数量1 | ColB2 | ColB2 | Description |说明 | Qty2数量2
    --A - - - - Apple - - - - -1 - - - - C - - - Banana - - - - 1 --A - - - - 苹果 - - - - -1 - - - - C - - - 香蕉 - - - - 1
    --C - - - - Banana - - - 1 - - - - D - - - Watermelon - 2 --C - - - - 香蕉 - - - 1 - - - - D - - - 西瓜 - 2
    --B - - - - Orange - - - 1 - - - - A - - - Apple - - - - - - 1 --B - - - - 橙色 - - - 1 - - - - A - - - 苹果 - - - - - - 1

Sorry about the formatting.对不起格式。 first time posting a questi第一次发布问题

You want to join the tables.你想join表格。 . . . . twice:两次:

select t2.cola2, t1_a.DescrpA1, t2.colb2, t1_b.DescrpA1
from table2 t2 join
     table1 t1_a
     on t2.cola2 = t1_a.cola1 join
     table1 t1_b
     on t2.colb2 = t1_b.cola1;

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

相关问题 SQL 查询将多列中的值映射到另一个表中的不同值 - SQL query map values in multiple columns to different value in another table 将一个查询的查询结果转换为另一个查询的列 - Turn query results from one query into columns of another 将 SELECT 列表中一个表的查询结果用于另一个表 - Using query results from one table in the SELECT list for another table 从一个表到另一列的不同表中检索数据 - Retrieving Data From One Table To Another Table With Different Columns 将一个表的查询结果与另一个表的默认值合并 - Combine query results from one table with the defaults from another 将一个汇总查询除以另一个表中的另一个汇总查询 - Divide one aggregate query by another aggregate query from a different table 将数据库中一个表中的特定行插入到具有不同列的另一个表中 - Insert specific rows from one table in database into another with different columns 从另一个查询的结果向查询添加列 - Adding columns to a query from the results of another query Mysql查询在多个条件和列上从一个表插入到另一个表 - Mysql Query for inserting from one table to another on Multiple conditions and columns SQL 查询从一个表中获取不同的结果并将这些结果与表中的另一列匹配 - SQL Query to get distinct results from one table and match those results to the another column in the table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM