处理 MySQL 中缺失的列
致力于将一堆旧的、更随心所欲的时间放在一起的数据库解析成一个新的模式。基本上它是每年一个数据库,数据库名称如 foo98、foo99、foo2000 等。
因此,对于最新的 foo 数据,我可以执行类似的操作
SELECT foo_person.mdname AS middle_name,
...
FROM foo_person, foo_place, foo_thing
当您返回 foo 数据库的早期版本时,不存储中间名。我尝试构建一种通用查询,例如:
SELECT IFNULL(foo_person.mdname, "") AS middle_name,
...
FROM foo_person, foo_place, foo_thing
但 MySQL 抱怨未知列 foo_person.mdname,这是完全合理的,因为它不存在。
有没有办法只用 MySQL 语法来处理不存在的列,或者我必须编写特定于数据库的导入查询?
Working on parsing a bunch of databases put together in an older, more freewheeling time into a new schema. Basically it's one database per year, with database names like foo98, foo99, foo2000, etc.
So for the most recent foo data, I can do something like
SELECT foo_person.mdname AS middle_name,
...
FROM foo_person, foo_place, foo_thing
As you get back into earlier versions of the foo database, middle name isn't stored. I've tried to build a kind of universal query, something like:
SELECT IFNULL(foo_person.mdname, "") AS middle_name,
...
FROM foo_person, foo_place, foo_thing
but MySQL complains about unknown column foo_person.mdname, which is entirely reasonable as it doesn't exist.
Is there some way to handle non-existent columns with just MySQL syntax, or will I have to write database-specific import queries?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
没有任何方法可以处理 sql 中不存在的列(而不是空列)。
您可以使用 information_schema 表判断该列是否存在,如下所示:
There isn't any way of handling a non-existent column in sql (as opposed to an empty column).
You can tell whether the column is there or not using the information_schema tables, like so:
是的,有办法。
让我们考虑这些数据库
您可以执行类似以下操作(我为 MS SQL Server 编写此操作)
Yes there is a way.
Let's consider these databases
You can do something similar the following (I wrote this for MS SQL Server)
您能否重命名表并在缺少列的位置创建视图?
不确定这是否是您正在寻找的,但我想我会推荐它。
Could you rename the tables and create views in their place with the missing columns?
Not sure if this is what you're looking for, but thought I would suggest it.
如果您正在使用异构数据库,我无论如何都会使用特定于数据库的导入查询。您可能需要加入一些列并删除一些列,并截断一些列等。
If you're working with heterogenous databases, I would use database-specific import queries anyways. You might need to join some columns and remove some, and truncate some, etc.
与其进行更复杂的 sql 查询,也许更好
更改 foo98 和 foo99 表以添加缺少的列。
例如,要将名为“mdname”的 varchar(30) 类型的列添加到 foo98:
然后,无论访问哪个表,您都可以放松并使用相同的简单 SQL 查询。
Instead of making more complex sql queries, perhaps it would be better to
alter the foo98 and foo99 tables to add in the missing columns.
For example, to add a column of type varchar(30) called "mdname" to foo98:
Then you can relax and use the same simple SQL query no matter which tabel is being accessed.