【问题标题】:MySQL Insert to two seperate tables with pivotMySQL 使用数据透视表插入两个单独的表
【发布时间】:2013-12-15 02:58:02
【问题描述】:

从 .csv 引入的单个表 (user_import) 开始

| Name       | Login | Email                | CustomA | CustomB |
+------------+-------+----------------------+---------+---------+
| John Smith | johns | john_smith@gmail.com | Blarg   | Narx    |
| Max Power  | maxp  | max_power@gmail.com  | Jarg    | Lipdo   |
+------------+-------+----------------------+---------+---------+

试图让它填充 Joomla!用户表

| id  | name       | username | email                | ...
| 514 | Super User | admin    | admin@gmail.com      | ...
| 515 | John Smith | johns    | john_smith@gmail.com | ...
| 516 | Max Power  | maxp     | max_power@gmail.com  | ...

并将任何自定义自定义字段插入到 user_profiles 表中

+---------+-------------------------------------+---------------+----------+
| user_id | profile_key                         | profile_value | ordering |
+---------+-------------------------------------+---------------+----------+
|     515 | customprofile.custom_a              | "Blarg"       |        1 |
|     515 | customprofile.custom_b              | "Jarg"        |        2 |
|     516 | customprofile.custom_a              | "Narx"        |        1 |
|     516 | customprofile.custom_b              | "Lipdo"       |        2 |
+---------+-------------------------------------+---------------+----------+

我认为没有办法在单个调用中执行此操作,因为 user_id 必须自动递增

第一个查询非常困难

INSERT INTO prknc_users (name, username, email, params, password)
SELECT Name, Login, Email, '{}', 'tuChaSw-tEte72_!eSW#muc3@trew8steZacra2e7a7R6yuqAyeSAXUy=Stu'
FROM user_import;`

第二个是我需要帮助的一个,尝试了一个:

INSERT INTO user_profiles (user_id, profile_key, profile_value, ordering)
SELECT (SELECT users.id FROM users, user_import WHERE users.email = user_import.Email), 'customprofile.custom_a',user_import.CustomA, '1'
FROM user_import;

努力失败。如果可以的话,请帮帮我。

【问题讨论】:

  • 您知道您还需要在用户组映射表中插入,对吧?
  • 是的,Joomla!当我不插入它们时不会生气,但如果我希望它们能够登录,我至少希望将它们分配到“已注册”组。如果我可以在 2-3 个查询中完成所有这些操作,我会很高兴,但我显然会满足于一些可行的方法。 |用户 ID | group_id | +---------+----------+ | 514 | 8 | | 515 | 2 | | 516 | 2 | +---------+---------+
  • 使用 api 而不是尝试编写自己的查询。我们有一个 API 是有原因的。我也会查看配置文件 API,IIRC 直接支持 JUser 或 JUserHelper 中的配置文件。
  • 在处理大量用户导入时,涉及使用 PHP 和 IMO 的工作量要大得多...不过,我对您的书非常感兴趣 :) 他们在加拿大的任何商店都出售吗?
  • 我的朋友在多伦多看到了,所以我猜是的!不,实际上它的工作量更少,正如您从尝试正确查询所花费的时间中看到的那样。您知道如何编写 joomla 命令行应用程序吗?然后,您可以阅读您的数据,正确插入所有内容。您可能真的很喜欢 Joomla 编程书。这是我为修复资产而编写的(Web)应用程序,只是为了让您了解如何处理它。 github.com/elinw/AssetFix/blob/j3/assetfix.php

标签: mysql joomla


【解决方案1】:

你可以试试这样的应用

https://gist.github.com/elinw/5b579e18b9613f08330d

只要确保做出对您的用例有意义的更改即可。

【讨论】:

  • 谢谢!一定会看看这个。
  • 终于开始使用它了,这绝对是更好的选择。很高兴您向我介绍了 CLI,因为我一直在使用它进行各种大众活动!如果没有您的示例代码,上手会困难得多。还买了一本你的书。再次感谢。
【解决方案2】:
INSERT INTO user_profiles (user_id, profile_key, profile_value, ordering)
  SELECT users.id,'customprofile.custom_b',user_import.CustomB, '2'
    FROM users, user_import 
    WHERE users.email = user_import.Email
UNION
  SELECT users.id,'customprofile.custom_a',user_import.CustomA, '1'
  FROM users, user_import
  WHERE users.email = user_import.Email;

【讨论】:

    【解决方案3】:

    内部选择可以返回多个值,并且不会与外部选择中的其他列不同步。您应该将最后三列移到第一个选择中。

    INSERT INTO user_profiles (user_id, profile_key, profile_value, ordering)
    (SELECT users.id, 'customprofile.custom_a',user_import.CustomA, '1' 
        FROM users, user_import 
        WHERE users.email = user_import.Email)
    

    【讨论】:

    • 仍然得到以下信息:错误 1267 (HY000):排序规则 (utf8_general_ci,IMPLICIT) 和 (utf8_unicode_ci,IMPLICIT) 用于操作 '=' 的非法混合
    • 两个表格中的电子邮件类型是什么?
    • 不得不改变我的 user_import 表 - ALTER TABLE user_import CONVERT TO CHARACTER SET utf8;也不再需要 SELECT 周围的 () 了。在上面发布了一个答案,允许您在一个查询中同时执行这两项操作。感谢您的帮助!
    • 很高兴它成功了。这是另一个类似的问题stackoverflow.com/questions/11770074/…
    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多