简体   繁体   中英

SQLite: execute case-sensitive LIKE query on a specific query

I want to run a SELECT ... LIKE query in SQLite that is case-sensitive. But I only want this one query to be case sensitive, and nothing else.

I know there is

PRAGMA case_sensitive_like = boolean;

But that seems to change all LIKE queries.

How can I enable case-sensitive LIKE on a single query?

Examples: I want a query for "FuN" to match "blah FuN blah", but not "foo fun bar".

(This is running under PHP using PDO)

I might be able to toggle that on, then off after the query but I can concerned about the repercussions that may have (efficiency etc). Is there any harm?

I don't have write access to the database.

(This is under Windows Server 2008)

I also tried SELECT id, summary, status FROM Tickets WHERE summary COLLATE BINARY LIKE '%OPS%'; but that did not do a case-sensitive SELECT, it still returned results returns like laptops .

Why not go the simple way of using

PRAGMA case_sensitive_like = true/false;

before and after each query you want to be case sensitve? But beware- case sensitivity does only work for ASCII characters, not Unicode which makes SQlite not fully UC-compliant at this time.

Alternatively, SQlite allows applications to implement the REGEXP operator which might help according to www.sqlite.org/lang_expr.html .

Try:

 SELECT id, summary, status FROM Tickets WHERE summary GLOB \"*OPS*\";

there is no space between * and OPS .

I think you may need to do a seperate check in your php code on returned value to see if they match your case-sensitive data.

$rs = mysql_query("Select * from tbl where myfield like '%$Value%'");


while($row == mysql_fetch_assoc($rs))
{

     if (strpos($row['myfield'],$Value) !== false)
     {
        $Matches[] = $row;
      }


 }

 print_R($Matches);

You can try something like this:

SELECT YOUR_COLUMN FROM YOUR_TABLE WHERE YOUR_COLUMN COLLATE latin1_general_cs LIKE '%YOUR_VALUE%'

Not sure what your collation set is on the column. I picked latin as an example. Run the query and change 'cs' to 'ci' at the end. You should see different results.

UPDATE

Sorry. read the question too fast. The above collation is for mysql. For SQLLite, you should be able to use BINARY which should give you case sensitive search.

ref: http://www.sqlite.org/datatype3.html#collation

You can do that per column, not per query (which may be your case). For this, use sqlite collations .

CREATE TABLE user (name VARCHAR(255) COLLATE NOCASE);

All LIKE operations on this column then will be case insensitive.

You also can COLLATE on queries even though the column isn't declared with a specific collation:

SELECT * FROM list WHERE name LIKE '%php%' COLLATE NOCASE

Note that only ASCII chars are case insensitive by collation. "A" == "a", but "Æ" != "æ"

SQLite allows you to declare new collation types with sqlite_create_collation and them implement the collation logic on the PHP side, but PDO doesn't expose this.

SELECT * FROM table WHERe field LIKE '%search_term%'

在这种形式中,SELECT不区分大小写。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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