简体   繁体   English

MySQL-如何从另一个表控制一个表的“数字”列

[英]MySQL - How to control `numeric` column of one table from another table

I have the following: 我有以下内容:

book(book_id,book_name,quantity)
donor(book_id)
acquisition(donor_id,book_id,booksDonated)

When I insert a data in acquisition, the value of booksDonated must update to the given book_id 's quantity . 当我在采集中插入数据时, booksDonated的值必须更新为给定book_idquantity I tried learning triggers, but it turns out it cannot pass variable, since i am working in PHP. 我尝试学习触发器,但事实证明,由于我在PHP中工作,因此它无法传递变量。
How can I insert and update(with the given data of the table) at the same time. 如何同时插入和更新(使用表的给定数据)。
PS. PS。 Please add a query of it if it's something complex. 如果复杂,请添加查询。

Here's a more complete answer. 这是一个更完整的答案。 First, you need to create the following table structure in your database (creating tables, primary keys and autoincrement fields). 首先,您需要在数据库中创建以下表结构(创建表,主键和自动增量字段)。

CREATE TABLE IF NOT EXISTS `acquisition` (
  `acquistion_id` bigint(20) NOT NULL,
  `donor_id` bigint(20) NOT NULL,
  `book_id` bigint(20) NOT NULL,
  `booksDonated` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `book` (
  `book_id` bigint(20) NOT NULL,
  `book_name` varchar(255) NOT NULL,
  `quantity` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `donor` (
  `donor_id` bigint(20) NOT NULL,
  `name` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `acquisition` ADD PRIMARY KEY (`acquistion_id`);

ALTER TABLE `book` ADD PRIMARY KEY (`book_id`);

ALTER TABLE `donor` ADD PRIMARY KEY (`donor_id`);

ALTER TABLE `acquisition` MODIFY `acquistion_id` bigint(20) NOT NULL AUTO_INCREMENT;

ALTER TABLE `book` MODIFY `book_id` bigint(20) NOT NULL AUTO_INCREMENT;

ALTER TABLE `donor` MODIFY `donor_id` bigint(20) NOT NULL AUTO_INCREMENT;

Now, based on this structure, you can use the following functions to 现在,基于此结构,您可以使用以下功能

Add a book Add a donor (2 different donor's in the example) Add a book's acquisition 添加书籍添加捐赠者(示例中为2个不同的捐赠者)添加书籍的获取

function addBook($con, $name)
{
    $id = -1;
    $name = mysqli_real_escape_string($con, $name);
    $query = "INSERT INTO `book` (`book_id`, `book_name`, `quantity`) VALUES (NULL, '$name', '0');";
    mysqli_query($con, $query);
    if (mysqli_error($con) == 0)
    {
    $id = mysqli_insert_id($con);
    }
    return $id;
}
function addDonor($con, $name)
{
    $id = -1;
    $name = mysqli_real_escape_string($con, $name);
    $query = "INSERT INTO `donor` (`donor_id`, `name`) VALUES ('1', '$name');";
    mysqli_query($con, $query);
    if (mysqli_error($con) == 0)
    {
    $id = mysqli_insert_id($con);
    }
    return $id;
}
function addAcquisition($con, $book_id, $donor_id, $quantity)
{
    $id = 0;
    $book_id = intval($book_id);
    $donor_id = intval($donor_id);
    $quantity = intval($quantity);
    $query = "INSERT INTO `acquisition` (`acquistion_id`, `donor_id`, `book_id`, `booksDonated`) VALUES (NULL, '{$donor_id}', '{$book_id}', '{$quantity}');";
    mysqli_query($con, $query);
    if (mysqli_error($con) != 0)
    {
    $id = -1;
    }

//This SQL updates the current book count with the newly donated quantity, so they stay in sync.

    $query = "UPDATE `book` SET `quantity` = (`quantity` + '{$quantity}') WHERE `book_id` = '{$book_id}';";
    mysqli_query($con, $query);

    if (mysqli_error($con) == 0)
    {
    $id = mysqli_insert_id($con);
    }
    return $id;
}

Now, using the functions, you can create a book, one or more donors and their acquisitions. 现在,使用这些功能,您可以创建一本书,一个或多个捐赠者及其收购。

// Fill your DB values here
$host = '';
$username = '';
$password = '';
$database = '';

// Connect to the DB
$con = mysqli_connect($host, $username, $password, $database);

// Add a book. $book_id contains the new book's ID
$book_id = addBook($con, 'This is the book\'s name');

// Add a donor. $book_id contains the new donor's ID
$donor1_id = addDonor($con, 'This is the first Donor\'s name');

// Add another book. $book_id contains the second donor's ID
$donor2_id = addDonor($con, 'This is the second Donor\'s name');

addAcquisition($con, $book_id, $donor1_id, 5);
addAcquisition($con, $book_id, $donor2_id, 10);

// After running this code, there'll be 15 in the quantity field (5 from Donor 1 + 10 from Donor 2) and 2 acquistion records

You can use an UPDATE query, like the following: 您可以使用UPDATE查询,如下所示:

UPDATE book SET Quantity = Quantity + 'BOOKSDONATED' WHERE book_id = ID

This way, you add BOOKSDONATED books (the number of books the donor is giving) to the existing quantity of books with book_id = ID 这样,您可以将BOOKSDONATED图书(捐助者提供的图书数量)添加到book_id = ID的现有图书数量中

You then insert a record on the acquisition table using the books_I'd and donor_I'd. 然后,您使用books_Id和donor_Id在获取表上插入一条记录。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM