2

这是一个初学者 php/mysql 问题,我已经用各种循环搞砸了几个小时,但我还没有达到目标。我在数据库中有两个表。一个包含书籍,一个包含书籍的库存信息。像这样:

id  title
1   A book
2   Another book
3   A third book

对于股票(book_stock)

id  book_id   warehouse_id   qty
1   1         1              12
2   1         2              45
3   2         3              22
4   3         1              78
5   1         3              15

我想读取表格,将 book 表中的 id 与 stock 表中的 book_id 匹配,然后在 html 表中输出摘要,显示书名以及不同仓库中每本书的数量。到目前为止,我尝试过的涉及循环 mysql 请求,这似乎会崩溃,因为它没有输出任何结果。我觉得解决方案很简单并且涉及数组,但我的技能缺乏......

这是我的非工作代码。该函数适用于手动输入的 1、2 甚至 20 个结果,但在循环时仅输出空白表格单元格。

//function to get stock based on book id number
function get_stock($book_row_number)
{
    //get BOOK from db
    $book_query="SELECT * FROM book";
    $book_result=mysql_query($book_query);

    //determine book info
    $book_id=mysql_result($book_result,$book_row_number,"id");
    $book_title=mysql_result($book_result,$book_row_number,"title");

    //get LONDON stock for this book from db
    $stock_query="SELECT qty FROM book_stock WHERE book_id='$book_id' AND warehouse_id='1'";
    $stock_result=mysql_query($stock_query);
    $stock_london=mysql_result($stock_result,0,"qty");

    //get USA stock for this book from db
    $stock_query="SELECT qty FROM book_stock WHERE book_id='$book_id' AND warehouse_id='2'";
    $stock_result=mysql_query($stock_query);
    $stock_usa=mysql_result($stock_result,0,"qty");

    //get GERMANY stock for this book from db
    $stock_query="SELECT qty FROM book_stock WHERE book_id='$book_id' AND warehouse_id='4'";
    $stock_result=mysql_query($stock_query);
    $stock_germany=mysql_result($stock_result,0,"qty");

    echo "<tr><td>$book_title</td><td>$stock_london</td><td>$stock_usa</td><td>$stock_germany</td></tr>\n";
}

//find number of rows in book list
$all_query="SELECT * FROM book";
$all_result=mysql_query($all_query);
$all_rows=mysql_numrows($all_result);

// run the function on all the rows
$i=0;
while ($i < $all_rows) {
    get_stock('$all_rows');
    $i++;
}

编辑:现在的代码

<?php

//set up connection
$user="username";
$password="paswd";
$database="database";
mysql_connect("localhost",$user,$password);
@mysql_select_db($database) or die( "Unable to select database");

//query
$book_query="SELECT
book_id,
title,
SUM(CASE WHEN warehouse_id = 1 THEN book_stock.qty ELSE 0 END) AS warehouse1,
SUM(CASE WHEN warehouse_id = 2 THEN book_stock.qty ELSE 0 END) AS warehouse2,
SUM(CASE WHEN warehouse_id = 3 THEN book_stock.qty ELSE 0 END) AS warehouse3,
SUM(CASE WHEN warehouse_id = 4 THEN book_stock.qty ELSE 0 END) AS warehouse4
FROM
book_stock
JOIN book ON book.id = book_stock.book_id
GROUP BY book_id, title";

//loop
$result = mysql_query($book_query);
if ($result) {
  while ($row = mysql_fetch_assoc($row)) {
    echo "<tr><td>" . htmlspecialchars($row['title']) . "</td><td>{$row['warehouse1']}</td><td>{$row['warehouse2']}</td><td>{$row['warehouse3']}</td><td>{$row['warehouse4']}</td></tr>";
  }
}
else echo mysql_error();



//close database
mysql_close();
?>
4

1 回答 1

1

如果将其构建为数据透视表,则可以在单个查询中执行此操作。这个想法是聚合将匹配SUM()的值加在一起,或者当每行不匹配时为 0,将所有仓库折叠成一行。qtywarehouse_id

SELECT
  SUM(CASE WHEN warehouse_id = 1 THEN qty ELSE 0 END) AS warehouse1,
  SUM(CASE WHEN warehouse_id = 2 THEN qty ELSE 0 END) AS warehouse2,
  SUM(CASE WHEN warehouse_id = 3 THEN qty ELSE 0 END) AS warehouse3,
  SUM(CASE WHEN warehouse_id = 4 THEN qty ELSE 0 END) AS warehouse4
FROM
  book_stock
WHERE book_id = $book_id

对于book_id = 1,这会产生如下结果:

warehouse1 warehouse2 warehouse3 warehouse4
       12        45          15          0

要为所有行中的书籍获取它,请添加GROUP BY

SELECT
  book_id,
  title,
  SUM(CASE WHEN warehouse_id = 1 THEN qty ELSE 0 END) AS warehouse1,
  SUM(CASE WHEN warehouse_id = 2 THEN qty ELSE 0 END) AS warehouse2,
  SUM(CASE WHEN warehouse_id = 3 THEN qty ELSE 0 END) AS warehouse3,
  SUM(CASE WHEN warehouse_id = 4 THEN qty ELSE 0 END) AS warehouse4
FROM
  book_stock
  JOIN book ON book.id = book_stock.book_id
GROUP BY book_id, title

然后 PHP 的输出是一个简单的while循环:

// Assuming you already opened your <table>
$result = mysql_query($the_big_query_above);
if ($result) {
  while ($row = mysql_fetch_assoc($result)) {
    echo "<tr><td>" . htmlspecialchars($row['title']) . "</td><td>{$row['warehouse1']}</td><td>{$row['warehouse2']}</td><td>{$row['warehouse3']}</td><td>{$row['warehouse4']}</td></tr>";
  }
}
else echo mysql_error();

// Then don't forget to close your </table>
于 2012-09-01T21:14:00.563 回答