如何在查询中实现SQL_BIG_SELECTS = 1?

时间:2014-09-23 17:39:26

标签: php mysql

我遇到了出现此错误的查询问题

消息:

SQLSTATE [42000]:语法错误或访问冲突:1104 SELECT将检查多于MAX_JOIN_SIZE行;检查你的WHERE并使用SET SQL_BIG_SELECTS = 1或SET MAX_JOIN_SIZE =#如果SELECT没问题

但我真的不知道如何将sql_big_select设置为查询(或其他地方)

代码

public static function getItems( $orderBy, $limit = null, $term_id = null ) 
{
    $query  = ' SELECT definitions.*, terms.term, COUNT(DISTINCT(likes.id)) AS likes, COUNT(DISTINCT(dislikes.id)) AS dislikes, 
               CASE WHEN COUNT(voted.id) > 0 THEN 1 ELSE 0 END AS voted, 
               GROUP_CONCAT(DISTINCT(tags.name) SEPARATOR ",") AS tags ';
    // Select all the definition information, and count the likes and dislike records
    // Get whether the user has voted also
    $query .= 'FROM definitions ';
    // From definitions

    $query .= 'INNER JOIN terms ON terms.id = definitions.term_id ';

    $query .= 'LEFT JOIN votes AS likes ON definitions.id = likes.definition_id AND likes.score = 1 ';
    // Join on the votes table by likes
    $query .= 'LEFT JOIN votes AS dislikes ON definitions.id = dislikes.definition_id AND dislikes.score = -1 ';
    // Join on the votes table again, but by dislikes
    $query .= 'LEFT JOIN votes AS voted ON definitions.id = voted.definition_id AND voted.ip = ? ';
    // Join on the votes table again, but for voting

    $query .= 'LEFT JOIN definition_tag ON definitions.id = definition_tag.definition_id ';
    $query .= 'LEFT JOIN tags ON tags.id = definition_tag.tag_id ';
    // Join on the tags table



    $query .= 'WHERE approved = 1 ';
    // Only get approved definitions matching the term

    if ( ! is_null( $term_id ) ) 
    {
        $query .= 'AND definitions.term_id = ? ';
    }

    $query .= 'GROUP BY definitions.id ';
    // Group by the definition id
    $query .= 'ORDER BY '.$orderBy;
    // And order by highest likes

    if ( ! is_null( $limit ) ) $query .= ' LIMIT '.$limit;
    // Add the limit

    $bindings = array( 

        Request::ip()

    );

    if ( ! is_null( $term_id ) ) 
    {
        $bindings[1] = $term_id;

    }

    return DB::query($query, $bindings);
}

0 个答案:

没有答案