首页 > 解决方案 > SQL SELECT ALL of 2 列,乘以表 1 中的另一列和表 2 中的 INSERT

问题描述

我有以下内容:

Customer_Table
ID  |   CustomerID     Balance      Multiplying_Factor
1   |   739         |   500     |   8
2   |   3049        |   230     |   6
3   |   591         |   1050    |   10
4   |   035         |   85      |   5

下一步 [将上述值插入 Trade_Table]

对于插入的每一行Trade_Table,我们将为以下列自动生成唯一值:

** TradeID(随机 6 位数字)

Trade_Table
ID  |   CustomerID      Stake       Turnover    ROI         Status          TradeID         Timestamp
1   |   739         |   250     |   2000    |   1750    |   Pending     |   750392      |   Currenttimestamp
2   |   3049        |   115     |   690     |   575     |   Pending     |   127543      |   Currenttimestamp
3   |   591         |   525     |   5250    |   4725    |   Pending     |   009765      |   Currenttimestamp

进一步的步骤

完成上述操作后,我想更新Balance. 对于我们上面的每个SQL 语句,还有什么?CustomerIDCustomer_TableSET Balance=StakeCustomerIDSET Multiplying_Factor=new_factor

**将为每一行new_factor自动生成value >5 but <12

我无法陈述我搜索过的各种来源,但请相信我,这超出了我的基本 SQL 知识,因为它与动态创建新值有关。我可以处理其余代码的大部分 PHP 部分,但目前高级 SQL 不是我的事。任何帮助将不胜感激。谢谢!

到目前为止,这是我写出来的:

    require("dbconnection.php"); // Require DB Details and Connection details

$conn->set_charset("utf8");// Set characters to UTF8

$sql = "SELECT CustomerID, Balance, Multiplying_Factor FROM Customer_Table WHERE Balance > 100";
$result = $conn->query($sql);

$GLOBALS['count'] = mysqli_num_rows($result);

if ($result->num_rows > 0) {

echo "<table id=\"t01\">
  <tr>
    <th>Customer ID</th>
    <th>Balance</th> 
    <th>Stake</th> 
    <th>Factor</th> 
    <th>Turnover</th> 
    <th>ROI</th>
  </tr>";
  // output data of each row
    while($row = $result->fetch_assoc()) {

        $GLOBALS['Stake'] = $row["Balance"] / 2;
        $GLOBALS['CustomerID'] = $row["CustomerID"];
        $GLOBALS['Balance'] = $row["Balance"];
        $GLOBALS['Multiplying_Factor'] = $row["Multiplying_Factor"];
        $GLOBALS['Turnover'] = $Stake * $Multiplying_Factor;
        $GLOBALS['ROI'] = $Turnover - $Stake;
  echo "<tr class=\"GridItems\">
    <td>$CustomerID</td>
    <td>$Balance</td>
    <td>$Stake</td>
    <td>$Multiplying_Factor</td>
    <td>$Turnover</td>
    <td>$ROI</td></tr>";
    }
  echo "</table>";
  }

现在,我可以使用我正在寻找的新值在表格上获取每一行(虽然我不需要它们在表格上,但我只是在创造一个视觉印象)。

我现在的主要问题是如何从上面的结果集中为每个 CustomerID 插入 Trade_Table。

我的插入语句:

$Insert = "INSERT into Trade_Table (`CustomerID`, `Stake`, `Turnover`, `ROI`) VALUES ('$CustomerID[$i]', '$Stake[$i]', '$Turnover[$i]', '$ROI[$i]')";
$result = $conn->query($Insert);

但这只会插入一行而不是全部。我该怎么做?如何一次插入多个相应的数据。我已经尝试过“for each”语句,但我无法做到正确。

标签: phpmysqlsql

解决方案


我会尽力根据我对您问题的理解来帮助您。在那之前:

I'm not a PhP guy -so no help there

I can help you with framing the MySQL query alone

我的理解是:

You want to manipulate the values in the 'customer' table and insert them into the 'trade' table

After the insert, you want to update the 'customer' table with the values from 'trade' table

以此为要求,您可以执行以下操作。

首先,我创建您的两个表:我使用您的值创建表“客户”。

mysql> select * from customer;
+------+------------+---------+--------------------+
| ID   | customerID | Balance | Multiplying_Factor |
+------+------------+---------+--------------------+
|    1 |        739 |     500 |                  8 |
|    2 |       3049 |     230 |                  6 |
|    3 |        591 |    1050 |                 10 |
|    4 |         35 |      85 |                  5 |
+------+------------+---------+--------------------+

我创建表“交易”。我不包括“状态/贸易ID/时间戳”列,因为您没有指定有关如何填充这些列的任何内容。目前这张桌子是空的,所以我只是描述一下。

mysql> desc trade;
+------------+---------+------+-----+---------+-------+
| Field      | Type    | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+-------+
| ID         | int(11) | YES  |     | NULL    |       |
| customerID | int(11) | YES  |     | NULL    |       |
| Stake      | double  | YES  |     | NULL    |       |
| Turnover   | double  | YES  |     | NULL    |       |
| ROI        | double  | YES  |     | NULL    |       |
+------------+---------+------+-----+---------+-------+
5 rows in set (0.00 sec)

我编写了您需要操作“客户”表中的值以获取股权/营业额/投资回报率的 SELECT 查询

mysql> select ID, customerID, (Balance/2) as Stake,     
((Balance/2)*Multiplying_Factor) as Turnover, 
(((Balance/2)*Multiplying_Factor)-(Balance/2)) as ROI from customer 
where Balance>100;
+------+------------+----------+-----------+-----------+
| ID   | customerID | Stake    | Turnover  | ROI       |
+------+------------+----------+-----------+-----------+
|    1 |        739 | 250.0000 | 2000.0000 | 1750.0000 |
|    2 |       3049 | 115.0000 |  690.0000 |  575.0000 |
|    3 |        591 | 525.0000 | 5250.0000 | 4725.0000 |
+------+------------+----------+-----------+-----------+
3 rows in set (0.00 sec)

现在进行插入:

insert into trade select ID, customerID, (Balance/2), 
((Balance/2)*Multiplying_Factor), (((Balance/2)*Multiplying_Factor)-
(Balance/2)) from customer where Balance>100;
Query OK, 3 rows affected (0.04 sec)
Records: 3  Duplicates: 0  Warnings: 0

'trade' 表中的 'SELECT' 显示您在问题中的预期结果:

mysql> select * from trade;
+------+------------+-------+----------+------+
| ID   | customerID | Stake | Turnover | ROI  |
+------+------------+-------+----------+------+
|    1 |        739 |   250 |     2000 | 1750 |
|    2 |       3049 |   115 |      690 |  575 |
|    3 |        591 |   525 |     5250 | 4725 |
+------+------------+-------+----------+------+
3 rows in set (0.00 sec)

现在对于 UPDATE 查询:

mysql> update customer join trade using(customerID) set Balance=Stake;
Query OK, 3 rows affected (0.03 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> select * from customer;
+------+------------+---------+--------------------+
| ID   | customerID | Balance | Multiplying_Factor |
+------+------------+---------+--------------------+
|    1 |        739 |     250 |                  8 |
|    2 |       3049 |     115 |                  6 |
|    3 |        591 |     525 |                 10 |
|    4 |         35 |      85 |                  5 |
+------+------------+---------+--------------------+
4 rows in set (0.00 sec)

这会更新“客户”表中的“余额”列。我没有为“multiplying_factor”编写更新查询,因为您在帖子中提到的这个字段“new_factor”没有很好的解释。

我希望这可以帮助你


推荐阅读