简体   繁体   English

我可以使用SUBSTR从mysqli数据库中选择数据来优化查询吗

[英]Can i select data from mysqli database using SUBSTR to refine the query

I am trying the use refine tools for a search on my website. 我正在尝试使用优化工具在我的网站上进行搜索。 The bit i'm stuck with is search by start letter. 我遇到的问题是按起始字母搜索。 For example i could use a wildcard '%X%' but his would return anything that contained the letter 'x'. 例如,我可以使用通配符'%X%',但是他将返回包含字母'x'的所有内容。 I read on few sites that SUBSTRING can be used in mysql queries 我在几个站点上读到可以在mysql查询中使用SUBSTRING

http://dev.mysql.com/ http://dev.mysql.com/

http://www.kirupa.com/ http://www.kirupa.com/

https://stackoverflow.com/questions/6302027 https://stackoverflow.com/questions/6302027

This is what I have so far but returns nothing. 到目前为止,这是我所拥有的,但是什么也没有返回。 There is data in the database that should return with the query. 数据库中有应与查询一起返回的数据。

public function refineUsersFollowers($user_id,$q){
    if($this->databaseConnection()){
        // get the users followers
        $state = array(1,2);
        $stmt = $this->db_connection->prepare("SELECT * FROM friends WHERE id_2 = :1 AND Friend_Request_State = :2 OR id_2 = :3 AND Friend_Request_State = :4");
        $stmt->bindParam(':1',  $user_id);
        $stmt->bindParam(':2',  $state[0]);
        $stmt->bindParam(':3',  $user_id);
        $stmt->bindParam(':4',  $state[1]);
        $stmt->execute();
        // format the SQL OR statements
        $sql = '';
        $ids = [];
        while($rows = $stmt->fetch(\PDO::FETCH_ASSOC)){
            array_push($ids,$rows['id_1']);
        }

        for($x = 0; $x < count($ids); $x++){
            if(count($ids) == 1){
                //if there is one result
                $sql.= ' user_id = :'.$x." AND SUBSTRING('first_name',0,1) = :".$x.$x;
            }else if($x == (count($ids) - 1)){
                // last entry
                $sql.= ' user_id = :'.$x." AND SUBSTRING('first_name',0,1) = :".$x.$x;
            }else{
                //continue loop
                $sql.= ' user_id = :'.$x." AND SUBSTRING('first_name',0,1) = :".$x.$x." OR";
            }
        }
        $stmt = $this->db_connection->prepare("SELECT * FROM account WHERE ".$sql);
        for($x = 0; $x < count($ids); $x++){
            $stmt->bindParam(':'.$x,$ids[$x]);
            $insert = $x.$x.'';
            $stmt->bindParam(':'.$insert,$q);
        }
        $stmt->execute();
        $results = $stmt->fetch(\PDO::FETCH_ASSOC);
        print_r($results);
        // check for followers that start with letter
    }
}

The first part of the function is fine, this gets an array of id's which is then placed together as an SQL string. 该函数的第一部分很好,它将获得一个id数组,然后将其作为SQL字符串放置在一起。 Is the SQL not returning results because SUBSTRING is not supported in this way? 由于不支持SUBSTRING,SQL是否不返回结果? If so is there a way of producing a query like this or would it be easier to pull every result from the database then check them in a different function? 如果是这样,是否可以生成这样的查询,还是更容易从数据库中提取每个结果,然后以其他函数进行检查?

You have two issues with this expression: 此表达式有两个问题:

SUBSTRING('first_name', 0, 1) = :".$x.$x;

First, substr() in SQL (in general) starts counting with 1 and not 0. So, the first argument should be 1. 首先,SQL中的substr()通常以1而不是0开始计数。因此,第一个参数应为1。

Second, you have the first argument in single quotes. 其次,第一个参数用单引号引起来。 So, at best, this would return the letter 'f' . 因此,充其量,这将返回字母'f' Here is a simple rule: Only use single quotes for string and date constants. 这是一个简单的规则:仅对字符串和日期常量使用单引号。 Never use single quotes to refer to column names. 切勿使用单引号引用列名。

There are several way to write what you want. 有几种方法可以编写所需的内容。 Here are three: 这是三个:

SUBSTRING(first_name, 1, 1) = $x
LEFT(first_name, 1) = $x
first_name like '$x%'

You query can be greatly simplified with the LIKE operator. 使用LIKE运算符可以大大简化查询。 This: 这个:

"AND SUBSTRING('first_name',0,1) = :".$x.$x;

can become this: 可以变成这样:

"AND first_name LIKE '".$x.$x."%'";

I'm not sure what the $x.$x is for, so I just left it in for illustrative purposes. 我不确定$x.$x是干什么的,所以我只是将其留作说明。

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

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