我在MySql(events和special_events)中有2个表没有相同数量的列(special_events有一个唯一的datetime列 - 排除UNION),但是我想将两个表合并在一起并返回组合结果生成以下共享列:title,parent和directory ...
SELECT title, parent, root
FROM events
WHERE root != "services"
ORDER BY RAND() LIMIT 10
我需要在这里使用什么类型的JOIN。
NB。 special_events还有title,parent,root列等。
答案 0 :(得分: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
答案 1 :(得分: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