【发布时间】:2016-09-05 04:29:06
【问题描述】:
我有两张表,两张表都有相同的列,但一张表有额外的列。我需要根据列从它们中选择唯一记录,即下例中的 profilelink 存在于两个表中。
我尝试了以下方式的联合:
SELECT * FROM t1
UNION
SELECT * FROM t2
但我认为它是检查表中的所有列,然后取出结果,但在我的情况下,两个表中的列数不同,我需要仅基于一列获取记录。所以有人可以在这个查询中帮助我。
表结构如下
表 1
id | name | marks | profilelink
1 | a | 10 | http://example.com/a
2 | b | 20 | http://example.com/b
3 | c | 30 | http://example.com/c
4 | d | 40 | http://example.com/d
表 2
id | name | marks | Division | Result | profilelink
1 | e | 10 | I | Pass | http://example.com/e
2 | f | 20 | II | Pass | http://example.com/f
3 | b | 30 | III | Pass | http://example.com/b
4 | c | 40 | IV | Fail | http://example.com/c
预期结果是
id | name | marks | Division | Result | profilelink
1 | a | 10 | null | null | http://example.com/a
2 | d | 40 | null | null | http://example.com/a
3 | e | 10 | I | Pass | http://example.com/e
4 | f | 20 | II | Fail | http://example.com/f
在上面的示例中,profilelink 在两个表中都很常见。结果顺序无关紧要。
【问题讨论】:
-
您能添加表格结构和预期输出吗?
-
列数是什么意思??
-
一个很好的经验法则是“永远不要使用
SELECT *” -
@Strawberry:谢谢你,但在这里我只是写了我的方法的例子。
-
@DanilaGanchar:我已经添加了表结构和预期结果。
标签: mysql