Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL从其他表中选择与键关联的值_Sql_Select_Mapping_Selection - Fatal编程技术网

SQL从其他表中选择与键关联的值

SQL从其他表中选择与键关联的值,sql,select,mapping,selection,Sql,Select,Mapping,Selection,我有一个映射表tableA key value ----------- 1 "John" 2 "George" 3 "Kate" 4 "loves" 5 "hates" col1 col2 col3 ------------------ 1 5 2 2 4 3 3 4 1 以及另一个tableB,它包含基于tableA key value ----------- 1

我有一个映射表
tableA

key   value
----------- 
1     "John"
2     "George"
3     "Kate"
4     "loves"
5     "hates"
col1   col2   col3
------------------
1      5      2
2      4      3
3      4      1
以及另一个
tableB
,它包含基于
tableA

key   value
----------- 
1     "John"
2     "George"
3     "Kate"
4     "loves"
5     "hates"
col1   col2   col3
------------------
1      5      2
2      4      3
3      4      1
我想编写一个选择查询,它将返回
表B
中的行,但替换为相应的值

e、 g.输出必须为:

John   | hates | George
George | loves | Kate
Kate   | loves | John

谢谢。

您可能应该将最后两项“爱”和“恨”放在一个单独的表中,因为它们代表的数据类型与其他3项不同

SELECT A1.value, A2.value, A3.value 
FROM tableB 
JOIN tableA as A1 ON tableB.col1 = A1.key 
JOIN tableA as A2 ON tableB.col2 = A2.key 
JOIN tableA as A3 ON tableB.col3 = A3.key;
以下是表格:

用户:

id  name
----------
 1  John
 2  Edward
 3  Kate
感受:

id  type
----------
 1  love
 2  hate
用户感受:

id  user1_id  feeling_id  user2_id
-----------------------------------
 1      1         2          2
 2      2         1          3
 3      3         1          1
以下是问题:

select `Ua`.`name`, `F`.`type`, `Ub`.`name` from `feelings_users` as `X`
left join `users` as `Ua` on `X`.`user1_id` = `Ua`.`id`
left join `feelings` as `F` on `X`.`feeling_id` = `F`.`id`
left join `users` as `Ub` on `X`.`user2_id` = `Ub`.`id`
它将输出:

name    type  name
--------------------
John    hate  Edward
Edward  love  Kate
Kate    love  John