如何让 PHP 处理我的 "WITH CTE as" SQL?
How to make PHP handeling my "WITH CTE as" SQL?
我将 SQL 查询添加到 PHP 代码中,但它选择回显“0 个结果”。
我注意到在 Sublime Text 编辑器中,所有 sql 代码都以相同的颜色显示(不正确),但是当我删除第一个单词“WITH CTE as (”时,它变成了多个- Sublime Text 中的颜色 - 但功能失败。
简而言之,如何让 PHP 处理我的 SQL? 谢谢。
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "
WITH CTE as ( SELECT *
, LEAD(total_weight, 1) OVER(
ORDER BY weight_date DESC
) AS prev_total_weight
, MIN(total_weight) OVER() AS lowest_weight
, MAX(total_weight) OVER() AS highest_weight
, FROM_UNIXTIME(weight_date, '%u') AS weight_week
, ROW_NUMBER() OVER(
ORDER BY weight_date DESC
) AS RowNum
FROM (
SELECT *, weight_start_week + weight_end_week AS total_weight
FROM YourTable
) t
)
SELECT
`_ID`, `weight_date`, `weight_start_week`, `weight_end_week`
weight_end_week
,total_weight
,prev_total_weight
,lowest_weight
,highest_weight
,weight_week
,CASE
WHEN total_weight > prev_total_weight THEN 'greater'
WHEN total_weight = prev_total_weight THEN 'equal'
ELSE 'less'
END AS comparison
,RowNum
FROM CTE
";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<table><tbody><tr><td>Latest:</td><td>" . $row["total_weight"]. "</td><td>Percent</td><td>up/down</td></td></tr><tr><td>Lowest:</td><td>" . $row["lowest_weight"]. "</td><td>" . $row["weight_week"]. "</td><td>Date:</td></tr><tr><td>Most:</td><td>" . $row["highest_weight"]. "</td><td>" . $row["weight_week"]. "</td><td>Date:</td></tr></tbody></table>";
}
} else {
echo "0 results";
}
$conn->close();
?>
您可以将 CTE 内联到主查询中,并将其用作子查询:
SELECT `_ID`, `weight_date`, `weight_start_week`, `weight_end_week`
total_weight, prev_total_weight, lowest_weight, highest_weight,
weight_week,
CASE ...
FROM
(
-- place CTE definition here
SELECT *, ...
ROW_NUMBER() OVER( ORDER BY weight_date DESC) AS RowNum
FROM ...
) t;
我将 SQL 查询添加到 PHP 代码中,但它选择回显“0 个结果”。 我注意到在 Sublime Text 编辑器中,所有 sql 代码都以相同的颜色显示(不正确),但是当我删除第一个单词“WITH CTE as (”时,它变成了多个- Sublime Text 中的颜色 - 但功能失败。 简而言之,如何让 PHP 处理我的 SQL? 谢谢。
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "
WITH CTE as ( SELECT *
, LEAD(total_weight, 1) OVER(
ORDER BY weight_date DESC
) AS prev_total_weight
, MIN(total_weight) OVER() AS lowest_weight
, MAX(total_weight) OVER() AS highest_weight
, FROM_UNIXTIME(weight_date, '%u') AS weight_week
, ROW_NUMBER() OVER(
ORDER BY weight_date DESC
) AS RowNum
FROM (
SELECT *, weight_start_week + weight_end_week AS total_weight
FROM YourTable
) t
)
SELECT
`_ID`, `weight_date`, `weight_start_week`, `weight_end_week`
weight_end_week
,total_weight
,prev_total_weight
,lowest_weight
,highest_weight
,weight_week
,CASE
WHEN total_weight > prev_total_weight THEN 'greater'
WHEN total_weight = prev_total_weight THEN 'equal'
ELSE 'less'
END AS comparison
,RowNum
FROM CTE
";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<table><tbody><tr><td>Latest:</td><td>" . $row["total_weight"]. "</td><td>Percent</td><td>up/down</td></td></tr><tr><td>Lowest:</td><td>" . $row["lowest_weight"]. "</td><td>" . $row["weight_week"]. "</td><td>Date:</td></tr><tr><td>Most:</td><td>" . $row["highest_weight"]. "</td><td>" . $row["weight_week"]. "</td><td>Date:</td></tr></tbody></table>";
}
} else {
echo "0 results";
}
$conn->close();
?>
您可以将 CTE 内联到主查询中,并将其用作子查询:
SELECT `_ID`, `weight_date`, `weight_start_week`, `weight_end_week`
total_weight, prev_total_weight, lowest_weight, highest_weight,
weight_week,
CASE ...
FROM
(
-- place CTE definition here
SELECT *, ...
ROW_NUMBER() OVER( ORDER BY weight_date DESC) AS RowNum
FROM ...
) t;