A
A
Alexey Babiev2017-07-01 09:24:55
MySQL
Alexey Babiev, 2017-07-01 09:24:55

How to select records from mySQL whose field value is contained at the beginning of a row?

There is a table in which the field "field" is equal to:

  • abc
  • def
  • ghk
  • abcdef
  • defghk

There is a string "abcdefghk"
You need to make a query that will select the following records:
  • abc
  • abcdef

that is, which match the beginning of the string "abcdefghk"
UPD: Generally speaking, the field value must be contained at the beginning of the string.
The field values ​​and the string can be anything. Here I just gave an abstract example.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Babiev, 2017-07-01
@axsmak

He asked himself, answered himself =) While explaining to Sasha Pushkarev what his mistake was, he solved the question himself. Doubts are only about the optimality of such a request
Answer:

SELECT * FROM test WHERE field = LEFT('abcdefghk',LENGTH(field));
1e550c5ff32f4f5c977f119c63d04c72.png

A
Alexander Pushkarev, 2017-07-01
@AXP-dev

SELECT ...
FROM ...
WHERE field REGEXP '^abc(.*?)$'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question