从表A中选择*,但在第1列中显示它与表B中的匹配项

时间:2015-01-28 12:19:34

标签: mysql

为措辞严厉的标题道歉,但这是一个例子:

表A

╔═══╦════════════╦═════════════╗
║   ║ title      ║ creator     ║
╠═══╬════════════╬═════════════╣
║ 1 ║ Lorem      ║ 1           ║
║ 2 ║ Ipsum      ║ 2           ║
║ 3 ║ Dolor      ║ 3           ║
╚═══╩════════════╩═════════════╝

表B

╔═══╦════════════╦═════════════╗
║   ║ id         ║ name        ║
╠═══╬════════════╬═════════════╣
║ 1 ║ 1          ║ Tom         ║
║ 2 ║ 2          ║ John        ║
║ 3 ║ 3          ║ Harry       ║
╚═══╩════════════╩═════════════╝

是否有可能从表A中选择所有的查询,但在结果中它会将tableA.creator链接到tableB.name并在结果中显示tableB.name而不是tableA.creator?

4 个答案:

答案 0 :(得分:2)

SELECT b.name FROM A a LEFT JOIN B b ON a.creator=b.id

答案 1 :(得分:1)

SELECT A.Title, B.Name
FROM  TableA as A
INNER JOIN TableB AS B on(A.Creator = B.Id)

答案 2 :(得分:0)

SELECT a.title, b.name FROM A a LEFT JOIN B b ON a.creator=b.id

OR

SELECT a.title, b.name
FROM  A as a
JOIN B AS b on a.creator = b.id

答案 3 :(得分:0)

尝试以下查询

select distinct A.title , B.name
from A join B
on A.creator = B.id