如何将 CTE 转换为普通查询?
How to convert CTE to normal query?
如何将其转换为普通查询?
WITH cte AS (
SELECT agentID,
SUM(bonus > 0) OVER (PARTITION BY agentID
ORDER BY `year` * 12 + `month`
RANGE BETWEEN 2 PRECEDING AND CURRENT ROW) flag
FROM test
)
SELECT agentID
FROM cte
WHERE flag = 3;
我需要转换这个,因为我认为mariadb与cte不兼容。我也不太熟悉 cte,我不知道如何将其分解为 php.
中的正常 sql 查询
更新:
我试着这样做 运行 cte
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "sample_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$stmt = $conn->query("SELECT agentID, bonus FROM (WITH cte AS (
SELECT DISTINCT agentID,
SUM(bonus > 0) OVER (PARTITION BY agentID
ORDER BY `year` * 12 + `month`
RANGE BETWEEN 2 PRECEDING AND CURRENT ROW) flag
FROM sample_tbl
)) where agentID = '10710' && flag = 3");
if($stmt->num_rows > 0){
echo "You are elligible to take a course!";
} else{
echo "You are not elligible to take a course!";
}
?>
但是不行,结果显示
"Fatal error: Uncaught mysqli_sql_exception: You have an error in your
SQL syntax; check the manual that corresponds to your MariaDB server
version for the right syntax to use near ') where agentID = '10710' &&
flag = 3' at line 7 in C:\xampp\htdocs\try\index.php:16 Stack trace:
#0 C:\xampp\htdocs\try\index.php(16): mysqli->query('SELECT agentID,...') #1 {main} thrown in C:\xampp\htdocs\try\index.php on
line 16"
确实MariaDB is compatible with CTEs,但是如果您出于任何原因不想处理ctes,您总是可以将其转换为子查询:
SELECT agentID
FROM (
SELECT agentID,
SUM(bonus > 0) OVER (PARTITION BY agentID
ORDER BY `year` * 12 + `month`
RANGE BETWEEN 2 PRECEDING AND CURRENT ROW) flag
FROM test ) agents_with_summed_bonus
WHERE flag = 3;
如果这个查询(而不是用 cte 构建的那个)对你不起作用,那么这意味着你的初始查询在你的表方面有一些错误。
再次更新:
它现在对我有用,这是我的最终代码:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "sample_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$stmt = $conn->query("SELECT DISTINCT agentID FROM (SELECT DISTINCT agentID,
SUM(bonus > 0) OVER (PARTITION BY agentID
ORDER BY `year` * 12 + `month`
RANGE BETWEEN 2 PRECEDING AND CURRENT ROW) flag from sample_tbl) as cte where agentID = '61599' && flag = 3");
if($stmt->num_rows > 0){
echo "You are elligible to take a course!";
} else{
echo "You are not elligible to take a course!";
}
?>
如何将其转换为普通查询?
WITH cte AS (
SELECT agentID,
SUM(bonus > 0) OVER (PARTITION BY agentID
ORDER BY `year` * 12 + `month`
RANGE BETWEEN 2 PRECEDING AND CURRENT ROW) flag
FROM test
)
SELECT agentID
FROM cte
WHERE flag = 3;
我需要转换这个,因为我认为mariadb与cte不兼容。我也不太熟悉 cte,我不知道如何将其分解为 php.
中的正常 sql 查询更新:
我试着这样做 运行 cte
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "sample_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$stmt = $conn->query("SELECT agentID, bonus FROM (WITH cte AS (
SELECT DISTINCT agentID,
SUM(bonus > 0) OVER (PARTITION BY agentID
ORDER BY `year` * 12 + `month`
RANGE BETWEEN 2 PRECEDING AND CURRENT ROW) flag
FROM sample_tbl
)) where agentID = '10710' && flag = 3");
if($stmt->num_rows > 0){
echo "You are elligible to take a course!";
} else{
echo "You are not elligible to take a course!";
}
?>
但是不行,结果显示
"Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') where agentID = '10710' && flag = 3' at line 7 in C:\xampp\htdocs\try\index.php:16 Stack trace: #0 C:\xampp\htdocs\try\index.php(16): mysqli->query('SELECT agentID,...') #1 {main} thrown in C:\xampp\htdocs\try\index.php on line 16"
确实MariaDB is compatible with CTEs,但是如果您出于任何原因不想处理ctes,您总是可以将其转换为子查询:
SELECT agentID
FROM (
SELECT agentID,
SUM(bonus > 0) OVER (PARTITION BY agentID
ORDER BY `year` * 12 + `month`
RANGE BETWEEN 2 PRECEDING AND CURRENT ROW) flag
FROM test ) agents_with_summed_bonus
WHERE flag = 3;
如果这个查询(而不是用 cte 构建的那个)对你不起作用,那么这意味着你的初始查询在你的表方面有一些错误。
再次更新:
它现在对我有用,这是我的最终代码:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "sample_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$stmt = $conn->query("SELECT DISTINCT agentID FROM (SELECT DISTINCT agentID,
SUM(bonus > 0) OVER (PARTITION BY agentID
ORDER BY `year` * 12 + `month`
RANGE BETWEEN 2 PRECEDING AND CURRENT ROW) flag from sample_tbl) as cte where agentID = '61599' && flag = 3");
if($stmt->num_rows > 0){
echo "You are elligible to take a course!";
} else{
echo "You are not elligible to take a course!";
}
?>