我有1~9的pm_message表,我想创建一个简化MySQL查询过程的视图。
我拥有的是
CREATE VIEW `pm_messages` AS
SELECT * FROM
`pm_messages_0`,
`pm_messages_1`,
`pm_messages_2`,
`pm_messages_3`,
`pm_messages_4`,
`pm_messages_5`,
`pm_messages_6`,
`pm_messages_7`,
`pm_messages_8`,
`pm_messages_9`;
我在douplicate列中遇到错误。没有记录是重复的,我想在视图中合并所有这些,我该怎么办?
答案 0 :(得分:0)
您编写了一个巨大的交叉连接。根据行数的不同,它可能不会在宇宙受到熵热死亡之前返回。
我几乎可以肯定你想要一个 union :
CREATE VIEW `pm_messages` AS
SELECT * FROM `pm_messages_0` union all
SELECT * FROM `pm_messages_1` union all
SELECT * FROM `pm_messages_2` union all
SELECT * FROM `pm_messages_3` union all
SELECT * FROM `pm_messages_4` union all
SELECT * FROM `pm_messages_5` union all
SELECT * FROM `pm_messages_6` union all
SELECT * FROM `pm_messages_7` union all
SELECT * FROM `pm_messages_8` union all
SELECT * FROM `pm_messages_9`;
如果所有表都具有相同的列数和类型,则此方法有效。如果没有,您必须明确选择列,以便每个选择返回相同数量和类型的列。