mysql - 连接mysql中具有相似列的两个表

标签 mysql sql join

我有一个关于连接 mysql 中的两个表的问题。

我有这个数据模型:

timeline:
structure: i.imgur.com/THmV5hp.jpg
data: i.imgur.com/YaabHtf.jpg

timeline_personal:
structure: i.imgur.com/H0VzTMw.jpg
data: i.imgur.com/rX3NhOb.jpg

我喜欢连接不同行中的两个表。我尝试使用左连接、内连接和右连接,但这是不可能的。结果在相似的行中检索了我。我喜欢 this 中所示的结果链接:

这是一个很难的问题:P

最佳答案

您可以使用以下查询获得所需的结果:

  select 
      concat('T-',timeline.TimelineId), timeline.UsuarioId, timeline.TimelineFecha, "" Timeline, timeline.Evento, timeline.JuegoId, 0 TimelinePadre 
  from 
      timeline 
  union 
  select 
     concat('TP-',timeline_personal.TimelineId), timeline_personal.UsuarioId, timeline_personal.TimelineFecha, timeline_personal.Timeline, "0" Evento, "0" JuegoId, timeline_personal.TimelinePadre 
  from 
     timeline_personal 
  order by UsuarioId

关于mysql - 连接mysql中具有相似列的两个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19423094/

相关文章:

mysql - 关于带子查询的多行插入问题

php - 无法定位php中的sql错误

mysql - 使用通配符搜索多对多关系,性能问题

php - 在mysql数据库中插入和获取不同的语言

sql - 在 sql 中选择将表与其自身连接的不同对

python - 你如何让 SQLAlchemy 覆盖 MySQL "on update CURRENT_TIMESTAMP"

python - 并非所有参数都在 SQL 语句中使用(Python、MySQL)

sql - 使用 SQL 遍历非整数行

mysql - 未正确加入

带有连接表字段排序的 Cakephp 分页不起作用