执行排名查询时mysql语法错误

发布于 2024-12-16 15:54:54 字数 804 浏览 0 评论 0原文

我想执行排名查询,但我不确定正确的语法是什么 这是我的查询:

  static public function sortranks(){
    global $db;
    $sql ="TRUNCATE TABLE `ranking`";
    $db->query($sql);

    $sql = "INSERT INTO `ranking` (`user_id`) VALUES 
    ( SELECT `employe_id` FROM `rates_employe` WHERE `status` = '0' ORDER BY rawpoint DESC ) ";
    $db->query($sql);

    $sql = "UPDATE  rates_employe , ranking  SET rates_employe.rank = ranking.rank WHERE 
    rates_employe.employe_id = ranking.user_id ";
    $db->query($sql);
    echo 'ok';
    exit;
  }

当我运行此查询时,我不断收到语法错误

数据库查询失败:您的 SQL 语法有错误;检查 与您的 MySQL 服务器版本相对应的手册 在 'SELECT employe_id FROM rates_employe WHERE 附近使用的语法 status = '0' ORDER BY rawpoint ' at line 2

i want to perform a ranking query but i'm not sure what is the right syntax
here is my query:

  static public function sortranks(){
    global $db;
    $sql ="TRUNCATE TABLE `ranking`";
    $db->query($sql);

    $sql = "INSERT INTO `ranking` (`user_id`) VALUES 
    ( SELECT `employe_id` FROM `rates_employe` WHERE `status` = '0' ORDER BY rawpoint DESC ) ";
    $db->query($sql);

    $sql = "UPDATE  rates_employe , ranking  SET rates_employe.rank = ranking.rank WHERE 
    rates_employe.employe_id = ranking.user_id ";
    $db->query($sql);
    echo 'ok';
    exit;
  }

i keep getting syntax error when i run this query

Database query failed: You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'SELECT employe_id FROM rates_employe WHERE
status = '0' ORDER BY rawpoint ' at line 2

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

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

发布评论

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

评论(2

活雷疯 2024-12-23 15:54:54

您应该一项一项地执行查询,而不是将它们连接在一起。

使用简单的赋值更改 .= 字符串连接,并在分配每个查询后执行它。如:

 $sql = "TRUNCATE TABLE ranking";
 $db->query($sql);
 $sql = "INSERT INTO `ranking`(`user_id`) VALUES ..."; 
 $db->query($sql);

同时从查询中删除VALUES:

插入排名 (user_id)
( SELECT employe_id FROM rates_employe WHERE status = '0' ORDER BY rawpoint DESC )

抱歉,如果我听起来很迂腐,但是是英语 Employee 在单词末尾拼写有两个 e

You should execute the queries one by one, instead of joining them together.

Change the .= string concatenation with a simple assignment, and after assigning each query, execute it. Such as:

 $sql = "TRUNCATE TABLE ranking";
 $db->query($sql);
 $sql = "INSERT INTO `ranking`(`user_id`) VALUES ..."; 
 $db->query($sql);

Also remove VALUES from the query:

INSERT INTO ranking (user_id)
( SELECT employe_id FROM rates_employe WHERE status = '0' ORDER BY rawpoint DESC )

Sorry if I sound pedantic, but in English Employee is spelt with two e at the end of the word.

苏大泽ㄣ 2024-12-23 15:54:54

尝试分别运行每个查询:

static public function sortranks(){
    global $db;
    $sql ="TRUNCATE TABLE ranking";
    $db->query($sql);

    $sql = "INSERT INTO `ranking`(`user_id`) VALUES 
    ( SELECT `employe_id` FROM `rates_employe` WHERE `status` = '0' ORDER BY `rawpoint` DESC ) ";
    $db->query($sql);

    $sql = "UPDATE  rates_employe , ranking  SET rates_employe.rank = ranking.rank WHERE 
    rates_employe.employe_id = ranking.user_id ";

    $db->query($sql);
    echo 'done';
    exit;
  }

Try running each queries separately:

static public function sortranks(){
    global $db;
    $sql ="TRUNCATE TABLE ranking";
    $db->query($sql);

    $sql = "INSERT INTO `ranking`(`user_id`) VALUES 
    ( SELECT `employe_id` FROM `rates_employe` WHERE `status` = '0' ORDER BY `rawpoint` DESC ) ";
    $db->query($sql);

    $sql = "UPDATE  rates_employe , ranking  SET rates_employe.rank = ranking.rank WHERE 
    rates_employe.employe_id = ranking.user_id ";

    $db->query($sql);
    echo 'done';
    exit;
  }

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