使用两个表SQL的字符串合并和替换表id

时间:2012-04-10 10:32:18

标签: mysql sql select

我遇到这种情况:

Table A:
+----+------------+
| id | text       |
+----+------------+
| 33 | str1       | 
| 34 | str2       | 
| 35 | str3       | 
| 36 | str4       | 
+----+------------+

Table B:
+----+--------+------+------------+----------+-------+
| id | title  | teme | year       | ed       | cont |
+----+--------+------+------------+----------+-------+
|  8 |     33 |   34 | 2012-04-06 |       35 |    36 | 
+----+--------+------+------------+----------+-------+

一个查询是否可以得到这个结果?:

+----+--------+------+------------+----------+-------+
| id | title  | teme | year       | ed       | cont |
+----+--------+------+------------+----------+-------+
|  8 |   str1 | str2 | 2012-04-06 |     str3 |  str4 | 
+----+--------+------+------------+----------+-------+

表A来自其他两个表之间的JOIN。

我使用的DBMS是Mysql

提前致谢

1 个答案:

答案 0 :(得分:3)

我唯一能想到的是

select b.id, 
       (select a.text from tableA a where a.id = b.title) as title, 
       (select a.text from tableA a where a.id = b.teme) as teme, 
       year, 
       (select a.text from tableA a where a.id = b.ed) as ed, 
       (select a.text from tableA a where a.id = b.cont) as cont
from tableB b
where b.id = 8