MySQL - 如何从另一个表控制一个表的`numeric`列

时间:2015-03-01 14:11:11

标签: php mysql

我有以下内容:

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

当我在采集中插入数据时,booksDonated的值必须更新为给定book_id的{​​{1}}。 我尝试学习触发器,但事实证明它无法传递变量,因为我在PHP中工作。
如何同时插入和更新(使用表格的给定数据)。
PS。如果它很复杂,请添加一个查询。

2 个答案:

答案 0 :(得分:1)

这是一个更完整的答案。首先,您需要在数据库中创建以下表结构(创建表,主键和自动增量字段)。

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;

现在,基于此结构,您可以使用以下功能

添加一本书 添加捐赠者(示例中有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;
}

现在,使用这些功能,您可以创建一本书,一个或多个捐赠者及其收购。

// 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

答案 1 :(得分:0)

您可以使用UPDATE查询,如下所示:

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

通过这种方式,您可以使用book_id = ID

将BOOKSDONATED图书(捐赠者提供的图书数量)添加到现有图书数量中

然后使用books_I'd和donor_I'd在记录表上插入记录。