如何将CTE转换为普通查询?

发布于 2025-01-31 09:06:30 字数 1686 浏览 4 评论 0原文

如何将其转换为普通查询?

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!";
        }




?>

,但它不起作用,结果显示

“致命错误:未接收的mysqli_sql_exception:您有错误 SQL语法;检查与您的MariadB服务器相对应的手册 适用于近距离使用的正确语法')ventID ='10710'&amp;&amp; flag = 3'在C:\ xampp \ htdocs \ try \ index.php中的第7行中的第7行:16堆栈跟踪: #0 C:\ Xampp \ htdocs \ try \ index.php(16):mysqli-&gt; query('select agentId,...')#1 {main}在c:\ xampp \ xampp \ htdocs \ htdocs \ try try try try \ index中。 .php打开 第16行

How can I convert this 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;

I need to convert this because I think mariadb is not compatible with cte. I am not really familiar with cte too and I don't have any idea how to break this down to normal sql query in php.

UPDATE:

I tried doing this to run the 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!";
        }




?>

but it is not working, the result shows

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦与时光遇 2025-02-07 09:06:30

的确, mariadb与CTES兼容,但是,如果您不想不想出于某种原因处理CTE,您总是可以将其转换为一个子查询:

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构建的查询,则对您不起作用,则意味着您的初始查询在关系中存在一些错误你的桌子。

Indeed MariaDB is compatible with CTEs, however if you don't want to deal with ctes for whatever reason, you can always transform it into a subquery:

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;

If this query, in place of the one built with cte, doesn't work for you, then it means that you're initial query has some mistakes in relation of your tables.

拍不死你 2025-02-07 09:06:30

再次更新:

现在对我有用,这是我的最终代码:

<?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!";
        }




?>

Update Again:

It is now working for me, here is my final code:

<?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!";
        }




?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文