I have a use case as below:
I have thousands of records in the database and let's say I am having one column named myValue.
Now the myValue's actual value can be an alphanumeric string where the first two characters are alphabets, the next 6 characters are numbers and the last character is a fixed alphabet let say 'x', which may be or may not be present in the value. (For Example 'AB123456','AB123456x')
So I know the format of the value for myValue field but not know all the actual values as there are lots of records.
Now I want to retrieve all such values for which the value without last character x (For Example, 'AB123456') and the same value with last character x (For Example, 'AB123456x') exists.
So is there any way I can retrieve such data?
I am right now doing trial and error on existing data but have not found success and there are thousands of such rows, so any help on this would be appreciated a lot.
You can do so like this:
SELECT myvalue
FROM t
WHERE myvalue LIKE '________'
AND EXISTS (
SELECT 1
FROM t AS x
WHERE x.myvalue = CONCAT(t.myvalue, 'x')
)
A (most likely) faster alternate is:
SELECT TRIM(TRAILING 'x' FROM myvalue) AS myvalue2
FROM t
GROUP BY myvalue2
HAVING COUNT(DISTINCT myvalue) > 1
I'm looking for a way to get a row from a tabla which have a column data type of string. This column could have values as follows:
1. "1,2,3,4,5"
2. "X,3,4,5,8"
3. "X,X,3,4,5"
4. "1,2,3,4,X"
5. "1,3,4,X,X"
and so on, ...
I want to accomplish a search for a String like
"1,2,3,4,5"
I tried with a
SELECT *
FROM *table_name*
WHERE *column* LIKE '%1,2,3,4,5%';
hoping this query could retrieve at least three results (in the example, first, third and forth strings) but it returns only the first string, because of course it's the only string that matches with the specified criteria. Anyone knows a way for me to accomplish this achievement?
I assume the X listed is literally the X character - if so, try
SELECT * FROM table WHERE '1,2,3,4,5' REGEXP REPLACE(column, 'X', '.')
I'd like to create a BETWEEN parameter that returns values that are alphabetic (i.e. between A-Z and a-z inclusive). Is there a way to do this without using two BETWEEN clauses?
You don't say what platform you are using
WHERE UPPER(fieldname) BETWEEN 'A' and 'Z'
or
WHERE UCASE(fieldname) BETWEEN 'A' and 'Z'
you might want to get rid of some pesky spaces
WHERE UPPER(TRIM(fieldname)) BETWEEN 'A' and 'Z'
This will be slow, we can't use fieldname in an index if we run a function on it. So we are forcing a tablescan, which means two betweens and an or will be faster if fieldname is indexed and the table has some number of rows.
I need to find in a row of my table all the values that are repetitive, e.g. 123text123text (there might be a line break between the repetitive sub-strings.
So I need an SQL query that tells PhpMyAdmin to return all instances were sub-string first half of value == sub-string second half of value.
--UPDATE I don't think i need regex for my query it just needs to select values from the row were sub-string from begin until half of string equals sub-string from half of string until end
Thanks!
ok it was as simple as
SELECT * FROM table_name WHERE substring(column, (length(column)/2)+1) like substring(column, 1, (length(column)/2)-1)
I've been trying to write this query, I need to select the rows where a column has only letters (a-z) and a full stop.
I tried this but it's not working:
SELECT * FROM table WHERE (c1 REGEXP '[^a-zA-Z\.]') = 0
This one would usually work in PHP.
Try:
SELECT * FROM table WHERE c1 REGEXP '^[a-zA-Z.]+$'
The anchor ^ and $ ensure that you are matching the entire string and not part of it. Next the character class [a-zA-Z.] matches a single upper/lower case letter or a period. The + is the quantifier for one or more repetitions of the previous sub-regex, so in this case it allows us to match one or more of either a period or a upper/lower case letter.
More info on regex usage in MySQL