How to retrieve the records those character star starting with A OR B OR C ,
I just tried this query
SELECT * FROM `test_tbl` WHERE Left(cus_name, 1) = 'a'
---> display all customer name starting with character a
AND I added one more condition, That is
SELECT * FROM `test_tbl` WHERE Left(cus_name, 1) = 'a' or Left(cus_name, 1) = 'b'
--->It displayed all customer name starting with character a and in some records middle i saw some name starting with b also,
What i want is , i want pull out records , which names are starting with A or B or C ,
And also it should order by alphabetical order,
Also i tried this below query.
SELECT * FROM `test_tbl` WHERE Left(cus_name, 1) REGEXP '^[^a-z]+$';
The rendered records for that above is , just two records started with A,
This above question for doing the alphabetical pagination ,
Thanks
You can try:
SELECT * FROM `test_tbl` WHERE cus_name REGEXP '^[abc]';
It will list all rows where cus_name starts with either a, b or c.
The regex used is ^[abc]:
^ : Is the start anchor
[..] : Is the character class. So [abc] matches either an a or a b or a c. It is equivalent to (a|b|c)
The regex you were using : ^[^a-z]+$
The first ^ is the start anchor.
[..] is character class.
The ^ inside the character class
negates it. So [^abc] is any
character other than the three
listed.
+ is the quantifier for one or
more.
$ is the end anchor.
So effectively you are saying: give me all the rows where cus_name contains one or more letters which cannot be any of the lowercase letters.
try like instead
SELECT * FROM `test_tbl` WHERE cus_name like 'a%'
Related
Using MySQL how can I query for all records that start with a certain letter ranging from one letter to another? For example I want to find all entries that have the first letter between a-f.
Matches:
Albert
Donald
Frank
Non Matches:
Sarah
Reba
Thomas
With numbers I can use
SELECT * FROM table WHERE id >= int AND id <= int
Or use the between statement. How can I do that using letters using the first letter of each word in the database?
You should be able to use a range here. To cover a through and including f, regardless of case, we can try:
SELECT *
FROM yourTable
WHERE (name >= 'a' AND name < 'g') OR (name >= 'A' AND name < 'G');
Demo
Note that this approach leaves open the possibility of MySQL being able to use an index which might exist on the name column.
As #John commented below, if you are not using a collation which is case sensitive, then we can simplify the above query to this:
SELECT *
FROM yourTable
WHERE name >= 'a' AND name < 'g';
You can use regular expression for this. Read more here: REGEX_LIKE(). The query you need will be like this:
MySQL 8.0
SELECT
*
FROM
<table_name>
WHERE
REGEXP_LIKE(<column_name>, '^[a-f]');
MySQL < 8.0
SELECT
*
FROM
<table_name>
WHERE
<column_name> REGEXP '^[a-f]';
This will match all register with starting with [a to f] range letters.
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
In my table I have firstname and last name. Few names are upper case ( ABRAHAM ), few names are lower case (abraham), few names are character starting with ucword (Abraham).
So when i am doing the where condition using REGEXP '^[abc]', I am not getting proper records. How to change the names to lower case and use SELECT QUERY.
SELECT * FROM `test_tbl` WHERE cus_name REGEXP '^[abc]';
This is my query, works fine if the records are lower case, but my records are intermediate ,my all cus name are not lower case , all the names are like ucword.
So for this above query am not getting proper records display.
I think you should query your database making sure that the names are lowered, suppose that name is the name you whish to find out, and in your application you've lowered it like 'abraham', now your query should be like this:
SELECT * FROM `test_tbl` WHERE LOWER(cus_name) = name
Since i dont know what language you use, I've just placed name, but make sure that this is lowered and you should retrieve Abraham, ABRAHAM or any variation of the name!
Hepe it helps!
Have you tried:
SELECT * FROM `test_tbl` WHERE LOWER(cus_name) REGEXP '^[abc]';
I don't know since when, but nowadays MySql REGEXP is case insensitive.
https://dev.mysql.com/doc/refman/5.7/en/pattern-matching.html
You don't need regexp to search for names starting with a specific string or character.
SELECT * FROM `test_tbl` WHERE cus_name LIKE 'abc%' ;
% is wildcard char. The search is case insensitive unless you set the binary attribute for column cus_name or you use the binary operator
SELECT * FROM `test_tbl` WHERE BINARY cus_name LIKE 'abc%' ;
A few valid options already presented, but here's one more with just regex:
SELECT * FROM `test_tbl` WHERE cus_name REGEXP '^[abcABC]';
SELECT *
FROM `thread`
WHERE forumid NOT IN (1,2,3) AND IF( LEFT( title, 1) = '#', 1, 0)
ORDER BY title ASC
I have this query which will select something if it starts with a #. What I want to do is if # is given as a value it will look for numbers and special characters. Or anything that is not a normal letter.
How would I do this?
If you want to select all the rows whose "title" does not begin with a letter, use REGEXP:
SELECT *
FROM thread
WHERE forumid NOT IN (1,2,3)
AND title NOT REGEXP '^[[:alpha:]]'
ORDER BY title ASC
NOT means "not" (obviously ;))
^ means "starts with"
[[:alpha:]] means "alphabetic characters only"
Find more about REGEXP in MySQL's manual.
it's POSSIBLE you can try to cast it as a char:
CAST('#' AS CHAR)
but i don't know if this will work for the octothorpe (aka pound symbol :) ) because that's the symbol for starting a comment in MySQL
SELECT t.*
FROM `thread` t
WHERE t.forumid NOT IN (1,2,3)
AND INSTR(t.title, '#') = 0
ORDER BY t.title
Use the INSTR to get the position of a given string - if you want when a string starts, check for 0 (possibly 1 - the documentation doesn't state if it's zero or one based).
I'd like to select rows from the database where the last character in the mov_id column equals to 1 and 2.
How would the query look like?
SELECT * FROM `myTable` WHERE `mov_id` LIKE '%1' OR `mov_id` LIKE '%2'
the % character is a wildcard which matches anything (like * in many other places)
If mov_id is a numeric value (TINYINT, INT, etc...) then you should use a numeric operator. For instance, use the modulo operator to keep the last digit
SELECT * FROM mytable
WHERE (mov_id MOD 10) IN (1, 2)
If mov_id is a string, you can use LIKE or SUBSTRING(). SUBSTRING() will be slightly faster.
SELECT * FROM mytable
WHERE SUBSTRING(mov_id, -1) IN ('1', '2')
If your table is big or that query is frequently run, you should definitely consider adding a column to your table, in which you would store mov_id's last digit/character, and index that column.
Try this way too:
SELECT field1
FROM table
WHERE RIGHT(field1, 1) = 'x'
it displays the fields that has last a value of x.
SELECT *
FROM Table
WHERE RIGHT(Column_name, 1) IN ('x')
if you want to match two character just replace 1 by 2.
In general:
RIGHT(COLUMN_NAME, NO_OF_CHARACTER_YOU WANT_TO_MATCH_FROM_LAST)
And if you want to match the starting char just use LEFT instead of RIGHT
You could also do something like:
select
your, fields, go, here
from table
where
substring(mov_id, (char_length(move_id) - 1)) = x
SELECT * FROM table WHERE mov_id REGEXP '1$' OR mov_id REGEXP '2$'