English 中文(简体)
MS Access 中自行的地图字段
原标题:Map fields to their own row in MS Access

我的任务是将数据从一个 MSSQL 服务器迁移到一个 MySQL 服务器。 我认为我比较容易先将 MSSQL 数据库转换为MS Access 数据库, 这样我就可以处理它, 然后将它导出为 MySQL 。

所以,我有一张桌子,叫做公司:

companyID |    name        |     c_phon  |  c_email                   | c_address
    1       StackOverflow     5555555555     [email protected]      NYC
    2       Google            5558675309     [email protected]           NYC

在新的数据库中,我们改变了这些字段的保存方式。我们没有使用每个行的电话号码、电子邮件和地址,而是使用预设表格。

前缀 :

prefID  |  prefName
  1         c_phone
  2         c_email
  3         c_address

我的问题是,我如何将每个字段转换成自己的一排, 在我们的Prefs公司桌子上。它应该看起来是这样的:

companyID | prefID | prefValue
    1         1      5555555555
    1         2      [email protected]
    1         3      NYC
    2         1      5558675309
    2         2      [email protected]
    2         3      NYC

我不知道怎么用标本什么的 所以我想用SQL做这个

INSERT INTO comanyPrefs (companyID, prefID, prefValue)
SELECT companyID, prefID, @fieldName
FROM companies, preferences
WHERE @fieldName = prefName

显然这不起作用。 我如何使用变量字段名称选择字段? 如何为每个字段插入一行?

最佳回答

您在正确的轨道上。 但是, 您需要插入每个可能的字段, 类似 :

INSERT INTO companyPrefs (companyID, prefID, prefValue)
SELECT companyID, (select prefID from prefs where prefName =  c_phone ), c_phone
FROM companies where c_phone is not null
UNION
SELECT companyID, (select prefID from prefs where prefName =  c_email ), c_email
FROM companies where c_email is not null
UNION
SELECT companyID, (select prefID from prefs where prefName =  c_address ), c_address
FROM companies where c_address is not null

http://www.sqlfiddle.com/#!3/5cf87/1, rel=“nofollow”>http://www.sqlfiddle.com/#!3/5cf87/1

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签