$sql = ('SELECT itemcode,itemname
GROUP_CONCAT(if(DAY(`date`) = 1, `quantity`, 0)) AS 1,
GROUP_CONCAT(if(DAY(`date`) = 2, `quantity`, 0)) AS 2,
GROUP_CONCAT(if(DAY(`date`) = 3, `quantity`, 0)) AS 3,
GROUP_CONCAT(if(DAY(`date`) = 4, `quantity`, 0)) AS 4,
GROUP_CONCAT(if(DAY(`date`) = 5, `quantity`, 0)) AS 5,
GROUP_CONCAT(if(DAY(`date`) = 6, `quantity`, 0)) AS 6,
GROUP_CONCAT(if(DAY(`date`) = 7, `quantity`, 0)) AS 7,
GROUP_CONCAT(if(DAY(`date`) = 8, `quantity`,0 )) AS 8,
GROUP_CONCAT(if(DAY(`date`) = 9, `quantity`, 0)) AS 9,
GROUP_CONCAT(if(DAY(`date`) = 10, `quantity`, 0)) AS 10,
GROUP_CONCAT(if(DAY(`date`) = 11, `quantity`, 0)) AS 11,
GROUP_CONCAT(if(DAY(`date`) = 12, `quantity`, 0)) AS 12,
GROUP_CONCAT(if(DAY(`date`) = 13, `quantity`, 0)) AS 13,
GROUP_CONCAT(if(DAY(`date`) = 14, `quantity`, 0)) AS 14,
GROUP_CONCAT(if(DAY(`date`) = 15, `quantity`, 0)) AS 15,
GROUP_CONCAT(if(DAY(`date`) = 16, `quantity`, 0)) AS 16,
GROUP_CONCAT(if(DAY(`date`) = 17, `quantity`, 0)) AS 17,
GROUP_CONCAT(if(DAY(`date`) = 18, `quantity`, 0)) AS 18,
GROUP_CONCAT(if(DAY(`date`) = 19, `quantity`, 0)) AS 19,
GROUP_CONCAT(if(DAY(`date`) = 20, `quantity`, 0)) AS 20,
GROUP_CONCAT(if(DAY(`date`) = 21, `quantity`, 0)) AS 21,
GROUP_CONCAT(if(DAY(`date`) = 22, `quantity`, 0)) AS 22,
GROUP_CONCAT(if(DAY(`date`) = 23, `quantity`, 0)) AS 23,
GROUP_CONCAT(if(DAY(`date`) = 24, `quantity`, 0)) AS 24,
GROUP_CONCAT(if(DAY(`date`) = 25, `quantity`, 0)) AS 25,
GROUP_CONCAT(if(DAY(`date`) = 26, `quantity`, 0)) AS 26,
GROUP_CONCAT(if(DAY(`date`) = 27, `quantity`, 0)) AS 27,
GROUP_CONCAT(if(DAY(`date`) = 28, `quantity`, 0)) AS 28,
GROUP_CONCAT(if(DAY(`date`) = 29, `quantity`, 0)) AS 29,
GROUP_CONCAT(if(DAY(`date`) = 30, `quantity`, 0)) AS 30,
GROUP_CONCAT(if(DAY(`date`) = 31, `quantity`, 0)) AS 31,
sum(`quantity`) AS TotalIssuedQuantity
FROM `issuerequest`
WHERE `date` BETWEEN 2019-02-01 AND 2019-02-31
GROUP BY itemcode
');
答案 0 :(得分:1)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = 'SELECT itemcode,itemname GROUP_CONCAT(if(DAY(`date`) = 1, `quantity`, 0)) AS 1, GROUP_CONCAT(if(DAY(`date`) = 2, `quantity`, 0)) AS 2, GROUP_CONCAT(if(DAY(`date`) = 3, `quantity`, 0)) AS 3, GROUP_CONCAT(if(DAY(`date`) = 4, `quantity`, 0)) AS 4, GROUP_CONCAT(if(DAY(`date`) = 5, `quantity`, 0)) AS 5, GROUP_CONCAT(if(DAY(`date`) = 6, `quantity`, 0)) AS 6, GROUP_CONCAT(if(DAY(`date`) = 7, `quantity`, 0)) AS 7, GROUP_CONCAT(if(DAY(`date`) = 8, `quantity`,0 )) AS 8, GROUP_CONCAT(if(DAY(`date`) = 9, `quantity`, 0)) AS 9, GROUP_CONCAT(if(DAY(`date`) = 10, `quantity`, 0)) AS 10, GROUP_CONCAT(if(DAY(`date`) = 11, `quantity`, 0)) AS 11, GROUP_CONCAT(if(DAY(`date`) = 12, `quantity`, 0)) AS 12, GROUP_CONCAT(if(DAY(`date`) = 13, `quantity`, 0)) AS 13, GROUP_CONCAT(if(DAY(`date`) = 14, `quantity`, 0)) AS 14, GROUP_CONCAT(if(DAY(`date`) = 15, `quantity`, 0)) AS 15, GROUP_CONCAT(if(DAY(`date`) = 16, `quantity`, 0)) AS 16, GROUP_CONCAT(if(DAY(`date`) = 17, `quantity`, 0)) AS 17, GROUP_CONCAT(if(DAY(`date`) = 18, `quantity`, 0)) AS 18, GROUP_CONCAT(if(DAY(`date`) = 19, `quantity`, 0)) AS 19, GROUP_CONCAT(if(DAY(`date`) = 20, `quantity`, 0)) AS 20, GROUP_CONCAT(if(DAY(`date`) = 21, `quantity`, 0)) AS 21, GROUP_CONCAT(if(DAY(`date`) = 22, `quantity`, 0)) AS 22, GROUP_CONCAT(if(DAY(`date`) = 23, `quantity`, 0)) AS 23, GROUP_CONCAT(if(DAY(`date`) = 24, `quantity`, 0)) AS 24, GROUP_CONCAT(if(DAY(`date`) = 25, `quantity`, 0)) AS 25, GROUP_CONCAT(if(DAY(`date`) = 26, `quantity`, 0)) AS 26, GROUP_CONCAT(if(DAY(`date`) = 27, `quantity`, 0)) AS 27, GROUP_CONCAT(if(DAY(`date`) = 28, `quantity`, 0)) AS 28, GROUP_CONCAT(if(DAY(`date`) = 29, `quantity`, 0)) AS 29, GROUP_CONCAT(if(DAY(`date`) = 30, `quantity`, 0)) AS 30, GROUP_CONCAT(if(DAY(`date`) = 31, `quantity`, 0)) AS 31, sum(`quantity`) AS TotalIssuedQuantity FROM `issuerequest` WHERE `date` BETWEEN 2019-02-01 AND 2019-02-31 GROUP BY itemcode ';
$result = mysqli_query($conn, $sql);
$data = [];
if (mysqli_num_rows($result) > 0) {
$data = mysqli_fetch_assoc($result);
}
mysqli_close($conn);
// now $data variable has array of your data, use $data and show data in html table using foreach loop
?>
在PDO中,您喜欢以下提到的内容:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";
$data = [];
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare('SELECT itemcode,itemname GROUP_CONCAT(if(DAY(`date`) = 1, `quantity`, 0)) AS 1, GROUP_CONCAT(if(DAY(`date`) = 2, `quantity`, 0)) AS 2, GROUP_CONCAT(if(DAY(`date`) = 3, `quantity`, 0)) AS 3, GROUP_CONCAT(if(DAY(`date`) = 4, `quantity`, 0)) AS 4, GROUP_CONCAT(if(DAY(`date`) = 5, `quantity`, 0)) AS 5, GROUP_CONCAT(if(DAY(`date`) = 6, `quantity`, 0)) AS 6, GROUP_CONCAT(if(DAY(`date`) = 7, `quantity`, 0)) AS 7, GROUP_CONCAT(if(DAY(`date`) = 8, `quantity`,0 )) AS 8, GROUP_CONCAT(if(DAY(`date`) = 9, `quantity`, 0)) AS 9, GROUP_CONCAT(if(DAY(`date`) = 10, `quantity`, 0)) AS 10, GROUP_CONCAT(if(DAY(`date`) = 11, `quantity`, 0)) AS 11, GROUP_CONCAT(if(DAY(`date`) = 12, `quantity`, 0)) AS 12, GROUP_CONCAT(if(DAY(`date`) = 13, `quantity`, 0)) AS 13, GROUP_CONCAT(if(DAY(`date`) = 14, `quantity`, 0)) AS 14, GROUP_CONCAT(if(DAY(`date`) = 15, `quantity`, 0)) AS 15, GROUP_CONCAT(if(DAY(`date`) = 16, `quantity`, 0)) AS 16, GROUP_CONCAT(if(DAY(`date`) = 17, `quantity`, 0)) AS 17, GROUP_CONCAT(if(DAY(`date`) = 18, `quantity`, 0)) AS 18, GROUP_CONCAT(if(DAY(`date`) = 19, `quantity`, 0)) AS 19, GROUP_CONCAT(if(DAY(`date`) = 20, `quantity`, 0)) AS 20, GROUP_CONCAT(if(DAY(`date`) = 21, `quantity`, 0)) AS 21, GROUP_CONCAT(if(DAY(`date`) = 22, `quantity`, 0)) AS 22, GROUP_CONCAT(if(DAY(`date`) = 23, `quantity`, 0)) AS 23, GROUP_CONCAT(if(DAY(`date`) = 24, `quantity`, 0)) AS 24, GROUP_CONCAT(if(DAY(`date`) = 25, `quantity`, 0)) AS 25, GROUP_CONCAT(if(DAY(`date`) = 26, `quantity`, 0)) AS 26, GROUP_CONCAT(if(DAY(`date`) = 27, `quantity`, 0)) AS 27, GROUP_CONCAT(if(DAY(`date`) = 28, `quantity`, 0)) AS 28, GROUP_CONCAT(if(DAY(`date`) = 29, `quantity`, 0)) AS 29, GROUP_CONCAT(if(DAY(`date`) = 30, `quantity`, 0)) AS 30, GROUP_CONCAT(if(DAY(`date`) = 31, `quantity`, 0)) AS 31, sum(`quantity`) AS TotalIssuedQuantity FROM `issuerequest` WHERE `date` BETWEEN 2019-02-01 AND 2019-02-31 GROUP BY itemcode ');
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
$data = $stmt->fetchAll();
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
?>
显示表中数据的HTML代码
<table>
<tr>
<th>itemcode</th>
<th>itemname</th>
</tr>
<?php foreach($data as $value) { ?>
<tr>
<td><?= $value['itemcode'] ?></td>
<td><?= $value['itemname] ?></td>
</tr>
<?php } ?>
<table>