简体   繁体   English

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

[英]How to convert CTE to normal query?

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.我需要转换它,因为我认为 mariadb 与 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.我也不太熟悉 cte,我不知道如何将其分解为 php 中的正常 sql 查询。

UPDATE:更新:

I tried doing this to run the cte我试着这样做来运行 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" “致命错误:未捕获的 mysqli_sql_exception:您的 SQL 语法有错误;请查看与您的 MariaDB 服务器版本对应的手册,了解在 ') where agentID = '10710' && flag = 3' 中第 7 行附近使用的正确语法C:\xampp\htdocs\try\index.php:16 堆栈跟踪:#0 C:\xampp\htdocs\try\index.php(16): mysqli->query('SELECT agentID,...') # 1 {main} 抛出 C:\xampp\htdocs\try\index.php 第 16 行"

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:事实上MariaDB 与 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;

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.如果此查询代替使用 cte 构建的查询对您不起作用,那么这意味着您的初始查询在与您的表相关时存在一些错误。

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




?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM