简体   繁体   English

尝试用其他两个表中的数据更新一个mySQL表失败

[英]Attempt to update one mySQL table with data from two other tables is failing

I am trying to update one table which holds the inputted data from my employees. 我正在尝试更新一个表,该表包含我的员工输入的数据。 The table has three columns: entry id, field id, and value. 该表包含三列:条目ID,字段ID和值。 I am wanting to make sure there is a row for every field in the this table according to each entry. 我想确保根据每个条目在此表中的每个字段都有一行。 As such I have the written the following php/sql script. 因此,我编写了以下php / sql脚本。 The thought process was get two arrays (one containing the entry ids and the other the field ids) and check the input table to see if there was an existing row for every field and entry. 思考过程是获得两个数组(一个包含条目ID,另一个包含字段ID),并检查输入表以查看每个字段和条目是否存在现有行。 If not then use an inert into command to add a row with the value 0, but this command is failing. 如果不是,则使用inert into命令添加值为0的行,但是此命令失败。 My guess is a timeout error due to the intensity of this function. 我的猜测是由于此功能的强度导致超时错误。 Any suggestions? 有什么建议么?

$db = JFactory::getDBO();

$field_query = 'SELECT id FROM `jos_directory_field`';
$db->setQuery( $field_query );
$fields = $db->loadObjectList();

$entry_query = 'SELECT id FROM `jos_directory_entry`';
$db->setQuery( $entry_query );
$entries = $db->loadObjectList();

  for($e=0;$e count($entries);$e++) {
    for($i=0;$i count($fields);$i++) {
      $insert_query = 'INSERT INTO `jos_directory_enf` (entry_id,field_id,field_value)';
      $insert_query .= 'SELECT '.$entries[$e]->id.','.$fields[$i]->id.',0';
      $insert_query .= 'FROM dual WHERE NOT EXISTS (SELECT * FROM `jos_directory_enf`';
      $insert_query .= 'WHERE entry_id = '.$entries[$e]->id.' and field_id = '.$fields[$i]->id.')';
      $db->setQuery( $insert_query );
      $db->query();
    }
  }

It's too bad Joomla doesn't appear to support prepared statements, as they can help with performance for repeated queries. 太糟糕了,Joomla似乎不支持准备好的语句,因为它们可以帮助提高重复查询的性能。

However, in this case there's a better option. 但是,在这种情况下,有一个更好的选择。 You should be able to replace the PHP code with a single SQL statement, making use of the IGNORE clause for INSERT statements. 您应该能够使用单个SQL语句替换PHP代码,并对INSERT语句使用IGNORE子句。 First, make sure you have a unique index on the entry_id and field_id columns of jos_directory_enf . 首先,确保你有一个唯一索引entry_idfield_idjos_directory_enf Then the SQL statement would be something like: 然后,SQL语句将类似于:

INSERT IGNORE INTO `jos_directory_enf` (entry_id,field_id,field_value)
  SELECT e.id, f.id, 0
  FROM jos_directory_entries AS e
    JOIN jos_directory_field AS f
;

.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL更新一个表,但使用其他两个表中的数据作为更新的一部分 - MySQL Update one table, but use data from two other tables as part of the update MySql根据另外两个表的条​​件在一个表中查找数据 - MySql find data in one table based on conditions of two other tables 从两个mysql表中获取数据,其中一个表的行与另一个表的行连接 - Get data from two mysql tables, where row of one table is connected to many in other mysql-将一个表与另外两个表联接 - mysql - join one table with two other tables 一次从两个表中检索数据,一个表引用另一个表 - Retrieving data from two tables at once with one table referencing the other PHP mysql如何连接两个表来从一个表链接名称和从其他表发布 - PHP mysql how to join two tables to link name from one table and post from other table MYSQL:在一张表中搜索用户ID以在其他2张表中搜索数据,然后显示所有3张表中的数据 - MYSQL: Search for User ID in one table to search for data in 2 other tables, then show data from all 3 tables 用每个数据更新两个mysql表 - update two mysql tables with data from each 连接MySQL中的两个表,其中一个表在另一个表中具有多个值 - Joining two tables in MySQL with one having multiple values in the other table 使用Codeigniter将表中的一行与MySQL中其他两个表的多行联接 - Join one row from a table with multiple rows of two other tables in MySQL using Codeigniter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM