为什么我不能在mysql(php)上使用LIMIT函数?

时间:2017-03-14 21:13:28

标签: php mysql

我有这个查询

import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

public class TestUtils {

  public static void getBeansFromSpringContext() {
    WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
    DataSource datasource  = (DataSource)context.getBean("someBean1");
    MySpringComponent springBean  = (MySpringComponent)context.getBean("someBean2");
  }
}   

我需要设置LIMIT

public function getGameHomes_limit($page,$limit){
    $query = sprintf('SELECT %1$sserver_homes.*,%1$sremote_servers.*, %1$sconfig_homes.*
        FROM `%1$sserver_homes` NATURAL JOIN `%1$sconfig_homes` NATURAL JOIN `%1$sremote_servers`; ',
        $this->table_prefix);
    return $this->listQuery($query);
}

但我有这个错误

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第2行的'NATURAL JOIN public function getGameHomes_limit($page,$limit){ $query = sprintf('SELECT %1$sserver_homes.*,%1$sremote_servers.*, %1$sconfig_homes.* FROM `%1$sserver_homes` LIMIT '.$page.','.$limit.' NATURAL JOIN `%1$sconfig_homes` NATURAL JOIN `%1$sremote_servers`; ', $this->table_prefix); return $this->listQuery($query); } NATURAL JOIN ogp_config_homes'附近使用正确的语法

1 个答案:

答案 0 :(得分:2)

更改查询的顺序,并使LIMIT成为最后一位:

public function getGameHomes_limit($page,$limit){
    $query = sprintf('SELECT %1$sserver_homes.*,%1$sremote_servers.*, %1$sconfig_homes.*
        FROM `%1$sserver_homes` NATURAL JOIN `%1$sconfig_homes` NATURAL JOIN `%1$sremote_servers` LIMIT '.$page.','.$limit.'; ',
        $this->table_prefix);
    return $this->listQuery($query);
}