[英]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_id
和field_id
列jos_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.