处理 MySQL 中缺失的列

发布于 2024-08-08 22:59:59 字数 508 浏览 6 评论 0原文

致力于将一堆旧的、更随心所欲的时间放在一起的数据库解析成一个新的模式。基本上它是每年一个数据库,数据库名称如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鹿港巷口少年归 2024-08-15 22:59:59

没有任何方法可以处理 sql 中不存在的列(而不是空列)。

您可以使用 information_schema 表判断该列是否存在,如下所示:

select * from information_schema.columns
    where table_name='mytable' and table_schema='mydatabase';

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:

select * from information_schema.columns
    where table_name='mytable' and table_schema='mydatabase';
惯饮孤独 2024-08-15 22:59:59

是的,有办法。

让我们考虑这些数据库

  • DB2009 有具有 Fname、MInitial 和 LName 的 Person
  • DB2008 有具有 Fname 和 LName 的 Person
  • DB2007 有具有 PersonName 的 Person

您可以执行类似以下操作(我为 MS SQL Server 编写此操作)

/*all three columns exist*/
SELECT FName, MInitial, LName
From DB2009.Person

UNION

/*one column is a forced null */
SELECT FName, NULL as MInitial, LName
From DB2008.Person

UNION

/*two columns are derived and one column is a forced null */
SELECT SubString (1, CharIndex (PersonName, ' '), PersonName) as FirstName,
       NULL as MInitial, 
       SubString (CharIndex (PersonName, ' '), len (PersonName), PersonName),
From DB2007.Person

Yes there is a way.

Let's consider these databases

  • DB2009 has Person with Fname, MInitial and LName
  • DB2008 has Person with Fname and LName
  • DB2007 has Person with PersonName

You can do something similar the following (I wrote this for MS SQL Server)

/*all three columns exist*/
SELECT FName, MInitial, LName
From DB2009.Person

UNION

/*one column is a forced null */
SELECT FName, NULL as MInitial, LName
From DB2008.Person

UNION

/*two columns are derived and one column is a forced null */
SELECT SubString (1, CharIndex (PersonName, ' '), PersonName) as FirstName,
       NULL as MInitial, 
       SubString (CharIndex (PersonName, ' '), len (PersonName), PersonName),
From DB2007.Person
你是暖光i 2024-08-15 22:59:59

您能否重命名表并在缺少列的位置创建视图?

不确定这是否是您正在寻找的,但我想我会推荐它。

-- Here is your original table
create table t (fname varchar(30), lname varchar(30));

-- Rename it to something else
alter table t rename to tOrig;

-- Create a view with the columns its missing that you need
create view t as select fname, lname, '' as mname from tOrig;

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.

-- Here is your original table
create table t (fname varchar(30), lname varchar(30));

-- Rename it to something else
alter table t rename to tOrig;

-- Create a view with the columns its missing that you need
create view t as select fname, lname, '' as mname from tOrig;
初见你 2024-08-15 22:59:59

如果您正在使用异构数据库,我无论如何都会使用特定于数据库的导入查询。您可能需要加入一些列并删除一些列,并截断一些列等。

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.

若有似无的小暗淡 2024-08-15 22:59:59

与其进行更复杂的 sql 查询,也许更好
更改 foo98 和 foo99 表以添加缺少的列。

例如,要将名为“mdname”的 varchar(30) 类型的列添加到 foo98:

ALTER TABLE foo98 ADD mdname varchar(30) AFTER first_name;

然后,无论访问哪个表,您都可以放松并使用相同的简单 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:

ALTER TABLE foo98 ADD mdname varchar(30) AFTER first_name;

Then you can relax and use the same simple SQL query no matter which tabel is being accessed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文