sql-like

How to use many LIKE operators and use index

醉酒当歌 提交于 2019-12-19 03:32:24
问题 In my query I want find rows that match one of many LIKE operators. I know 3 ways of doing that but only one of them can use index. Lets start with table: CREATE TABLE dir ( id BIGSERIAL PRIMARY KEY, path TEXT NOT NULL ); CREATE INDEX path_idx ON dir(path TEXT_pattern_ops); After inserting sample data I can do: EXPLAIN ANALYZE SELECT id, path FROM dir WHERE path LIKE 'A%' OR path LIKE 'B%' OR path LIKE 'C%'; Above query use index correctly. Second way: EXPLAIN ANALYZE SELECT id, path FROM dir

How to use many LIKE operators and use index

耗尽温柔 提交于 2019-12-19 03:32:19
问题 In my query I want find rows that match one of many LIKE operators. I know 3 ways of doing that but only one of them can use index. Lets start with table: CREATE TABLE dir ( id BIGSERIAL PRIMARY KEY, path TEXT NOT NULL ); CREATE INDEX path_idx ON dir(path TEXT_pattern_ops); After inserting sample data I can do: EXPLAIN ANALYZE SELECT id, path FROM dir WHERE path LIKE 'A%' OR path LIKE 'B%' OR path LIKE 'C%'; Above query use index correctly. Second way: EXPLAIN ANALYZE SELECT id, path FROM dir

Simulating LIKE in PHP

一笑奈何 提交于 2019-12-19 03:07:18
问题 Is there a way to simulate the LIKE operator of SQL in PHP with the same syntax? ( % and _ wildcards and a generic $escape escape character)? So that having: $value LIKE $string ESCAPE $escape you can have a function that returns the PHP evaluation of that without using the database? (consider that the $value , $string and $escape values are already set). 回答1: OK, after much fun and games here's what I have come up with: function preg_sql_like ($input, $pattern, $escape = '\\') { // Split the

Hibernate HQL query by using like operator

…衆ロ難τιáo~ 提交于 2019-12-18 18:53:50
问题 Seu the following mapping @Entity public class User { private Integer id; @Id; private Integer getId() { return this.id; } } Notice id is an Integer. Now i need this HQL query by using like operator Query query = sessionFactory.getCurrentSession().createQuery("from User u where u.id like :userId"); ATT: IT IS like operator NOT = (equals operator) Then i use List<User> userList = query.setParameter("userId", userId + "%").list(); But does not work because Hibernate complains

'LIKE ('%this%' OR '%that%') and something=else' not working

☆樱花仙子☆ 提交于 2019-12-18 18:36:01
问题 I have a select query where I am trying to search strings for multiple patterns LIKE ('%this%' or '%that%' ) and something=else Returns zero results However LIKE '%this%' and something=else returns results and LIKE '%that%' and something=else returns result Is it possible to get all my results into one query? If a string matches both, how will it handle that? 回答1: It would be nice if you could, but you can't use that syntax in SQL. Try this: (column1 LIKE '%this%' OR column1 LIKE '%that%')

doctrine2 dql, use setParameter with % wildcard when doing a like comparison

南楼画角 提交于 2019-12-18 10:40:56
问题 I want to use the parameter place holder - e.g. ?1 - with the % wild cards. that is, something like: "u.name LIKE %?1%" (though this throws an error). The docs have the following two examples: 1. // Example - $qb->expr()->like('u.firstname', $qb->expr()->literal('Gui%')) public function like($x, $y); // Returns Expr\Comparison instance I do not like this as there is no protection against code injection. 2. // $qb instanceof QueryBuilder // example8: QueryBuilder port of: "SELECT u FROM User u

SQL Like and like

只愿长相守 提交于 2019-12-18 09:22:33
问题 Is it possible to string together multiple SQL LIKE wildcards in one query - something like this? LIKE '% aaaa %' AND LIKE '% bbbb %' The aim is to find records that contain both wild cards but in no specific order. 回答1: The correct SQL syntax is: field LIKE '% aaaa %' AND field LIKE '% bbbb %' 回答2: Yes, that will work, but the syntax is: Field LIKE '%aaa%' AND field LIKE '%bbb%' 回答3: This is useful when you are using this statement with variables. SELECT * FROM `tableName` WHERE `colName`

PostgreSQL Reverse LIKE

扶醉桌前 提交于 2019-12-18 09:22:30
问题 I need to test if any part of a column value is in a given string, instead of whether the string is part of a column value. For instance: This way, I can find if any of the rows in my table contains the string 'bricks' in column : SELECT column FROM table WHERE column ILIKE '%bricks%'; But what I'm looking for, is to find out if any part of the sentence " The ships hung in the sky in much the same way that bricks don’t " is in any of the rows. Something like: SELECT column FROM table WHERE

Sql Like to RegEx

…衆ロ難τιáo~ 提交于 2019-12-18 07:45:04
问题 Is there a good way to convert Regular Expression into LIKE inside a Function (MSSQL)? The sproc does not get more complicated than this: (country\=)(?<Country>[\w\d]+)(\&sessionid\=)(?<SessionId>.+) The groups will not be used in the LIKE, they are there for another purpose. I would like to use this inside a sproc late like: SELECT * FROM [Table] WHERE test_regex(regex, 'text') = 1 Where the regex is a part of [Table] 回答1: It is possible to add Regular Expression parsing to SQL server, using

How to escape string while matching pattern in PostgreSQL

与世无争的帅哥 提交于 2019-12-18 06:58:36
问题 I want to find rows where a text column begins with a user given string, e.g. SELECT * FROM users WHERE name LIKE 'rob%' but "rob" is unvalidated user input. If the user writes a string containing a special pattern character like "rob_", it will match both "robert42" and "rob_the_man". I need to be sure that the string is matched literally, how would I do that? Do I need to handle the escaping on an application level or is it a more beautiful way? I'm using PostgreSQL 9.1 and go-pgsql for Go.