0

我在 MySql 中有 2 个表(events 和 special_events),它们的列数量不同(special_events 有一个唯一的 datetime 列 - 排除了 UNION),但是我想将这两个表合并在一起并返回组合结果生成以下共享列:标题、父级和目录...

SELECT title, parent, root 
FROM events 
WHERE root != "services" 
ORDER BY RAND() LIMIT 10

我需要在这里使用什么样的 JOIN。

注意。special_events 还具有标题、父级、根列等。

4

2 回答 2

2

您可能需要这样的查询:

SELECT *
FROM (
  SELECT title, parent, root 
  FROM events
  WHERE root != 'services'
  UNION ALL
  SELECT title, parent, root 
  FROM special_events
  WHERE root != 'services'
) s
ORDER BY RAND()
LIMIT 10
于 2013-07-29T10:10:06.223 回答
1

您还可以从 special_events 中获取唯一的日期时间列,例如,

SELECT *
FROM (
  SELECT title, parent, root, NULL as datetime_column
  FROM events
  WHERE root != 'services'
  UNION ALL
  SELECT title, parent, root, datetime_column
  FROM special_events
  WHERE root != 'services'
) s
ORDER BY RAND()
LIMIT 10
于 2013-07-29T10:14:21.973 回答