如何在一个查询中更新两个表? [英] How can I update two tables in one query?

查看:153
本文介绍了如何在一个查询中更新两个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在一个查询中更新两个表?下面是我的代码示例。如何将这两个更新查询放在一个?提前感谢!

There is any way to update two tables in one query? Below is an example of my code. How can I put those two update queries in one? Thank you in advance!

   <?php
// DATABASE UPDATE
   if (isset($_POST['submit']) or isset($_GET['submit'])){

// 1st QUERY
   $db =& JFactory::getDBO();
   $query_1 = "UPDATE table_1
                  SET name     = '".$_POST["name"]."',
                      surename = '".$_POST["surename"]."'
               WHERE id=1";
   $db->setQuery($query_1);
   $db->query();

// 2nd QUERY
   $db =& JFactory::getDBO();
   $query_2 = "UPDATE table_2
                  SET team_id   = '".$_POST["team_id"]."',
                  SET team_name = '".$_POST["team_name"]."'
                  ";
   $db->setQuery($query_2);
   $db->query(); } ?>


推荐答案

MySQL实际上允许更新单个

MySQL does actually allow updates to multiple tables in a single query (although often it makes sense for your application to do one at a time).

UPDATE table_1, table_2
SET table_1.field = <some value>, table_2.field = <some value>
WHERE table_1.field2 = table_2.field_2 
AND table_1.field_3 = <some other value>

请参阅: http://dev.mysql.com/doc/refman/5.1/en/update.html

正如其他人所说,你应该使用预备语句。

As other people have stated, you should look at using prepared statements.

这篇关于如何在一个查询中更新两个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆