简体   繁体   English

如何在一个查询中更新来自不同表的两个记录?

[英]how to update two records from different table in one query?

I'm building my e-commerce project for school, but i'm facing a problem. 我正在为学校建立我的电子商务项目,但是我遇到了一个问题。 How to reduce stock automatically and change the order status in one query? 如何在一个查询中自动减少库存并更改订单状态?

here's my database : 这是我的数据库:

table orders: id | 表订单:id | customer_id | customer_id | date | 日期| status | 状态| total

table orderitems: id | 表订单项:id | order_id | order_id | product_id | product_id | quantity 数量

table products: id | 餐桌产品:id | category | 类别| name | 名称| description | 描述| image | 图片 price | 价格| stock 股票

Here's the code for viewing records from table orders : 这是用于查看表订单记录的代码:

<?php session_start();
  ob_start();
  if(ISSET($_SESSION['SESS_ADMINLOGGEDIN'])){
  }
  else{
    header("location: login.php");
  }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" /    >
  <link rel="stylesheet" href="css/style_admin.css" type="text/css" />
  <title>Untitled Document</title>
</head>

<body>
  <?php
    require("../config.php");
    require("../functions.php");
    if(!isset($_SESSION['SESS_ADMINLOGGEDIN'])) {
      header("Location: " . $config_basedir);
    }
    if(isset($_GET['func']) == TRUE) {

      $funcsql = "UPDATE orders SET status = 10 WHERE id = " . $_GET['id'];
      mysql_query($funcsql);

      header("Location: orders.php");
    }
    else {
      require("header.php");
      echo "<h1>Outstanding orders</h1>";
      $orderssql = "SELECT * FROM orders WHERE status = 2";
      $ordersres = mysql_query($orderssql);
      $numrows = mysql_num_rows($ordersres);
      if($numrows == 0)
      {
        echo "<strong>No orders</strong>";
      }
      else
      {
        echo "<table cellspacing=10>";
        while($row = mysql_fetch_assoc($ordersres))
        {
          echo "<tr>";
          echo "<td>[<a href='adminorderdetails.php?id=" . $row['id']. "'>View</   a>]</td>";
          echo "<td>". date("D jS F Y g.iA", strtotime($row['date'])). "</td>";
          echo "<td>";
          if($row['registered'] == 1)
          {
            echo "Registered Customer";
          }
          else
          {
            echo "Non-Registered Customer";
          }
          echo "</td>";
          echo "<td>Rp. ;" . sprintf('%.2f',
          $row['total']) . "</td>";
          echo "<td>";
          if($row['payment_type'] == 1)
          {
            echo "PayPal";
          }
          else
          {
            echo "Cheque";
          }
          echo "</td>";
          echo "<td><a href='orders.php?func=conf&id=" . $row['id']. "'>Confirm    Payment</a></td>";
          echo "</tr>";
        }
        echo "</table>";
      }
    }
  ?>

</body>
</html>

i'm successful for updating status to '10' but i want to reduce stock by stock from table products - quantity from table orderitems when the administrator click the 'Confirm Payment'. 我已成功将状态更新为“ 10”,但我想减少表产品的库存量-当管理员单击“确认付款”时,表订单项的数量减少。

You can do it like the sample code below. 您可以像下面的示例代码那样进行操作。

Assume there are two tables. 假设有两个表。

Table 1 表格1

create table db1 (
username varchar(10),
user_level integer
);

Table 2 表2

create table db2 (
username varchar(10),
user_level integer
);

For sample data inserting few records with values. 对于样本数据,请插入一些带有值的记录。

insert into db1 values ('a', 0), ('b', 2), ('c', 3); 
insert into db2 values ('a', 2), ('b', 1), ('c', 3);

Here is the single query which you asked for as sample below 这是您在以下示例中要求的单个查询

update db1 inner join db2 on db1.username = db2.username 
   set db1.user_level = 6,
       db2.user_level = 5
  where db1.username = 'a';

You can´t update different tables in the same query. 您不能在同一查询中更新不同的表。 If you want to do so and prevent an inconsistency in the db because of the 2nd update failing you should use transactions otherwise you should use 2 queries. 如果要这样做并防止由于第二次更新失败而导致数据库不一致,则应使用事务,否则应使用2个查询。 You can implement transactions in your code, you may implement a trigger on update or you can create a stored procedure in the DB. 您可以在代码中实现事务,可以在更新时实现触发器,也可以在数据库中创建存储过程。

Wish i made myself clear and it helps! 希望我能使自己清楚,这会有所帮助!

暂无
暂无

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

相关问题 如何从Laravel中的两个不同页面更新一个表 - How to update one table from two different pages in laravel 如何从 php 中的一个查询更新两个表 - how to update two table from one query in php 如何在mysql中通过一个查询从两个不同的表中选择两个记录 - How to select two record from two different table with one query in mysql 无法将记录从一个表更新到另一个表 - Can not update records from one table into another 更新查询两个不同表中的两个不同列-MySQL - Update query for two different Column in two different table-MySQL MySQL查询从数据库中选择一个值等于两个不同事物之一的记录 - MySQL Query Selecting records from a database where a value equals one of two different things 如何使来自同一表的两个$ db-&gt; query具有按列彼此独立地位于一个php页面中的两个不同ORDER? - How to make two $db->query from the same table with two different ORDER by column that each other stand independenty in one php page? 我需要更新连接两个不同表的表。 此查询合适吗? - I need to update a table joining two different tables. Is this query the appropriate one? 如何从PHP MySQL中的两个不同表更新表? - How to update table based from two different tables in php MySQL? 如何通过将一个表中的 id 与另一个表匹配来选择和更新一个表中的记录? - How to select and update records in one table by matching ids from it with another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM