How can I use SQL like to only display single words that are in a particular column of any row? For example, suppose I have the following rows in column1.
Column1
put return
need return
got return
bring return
server
client
The single-words output I would like is:
server
client
Can this be accomplish using like? I know this can be accomplished using regular expressions, but if possible, I would prefer using LIKE.
You can use not like:
where col not like '% %'
SELECT * FROM YourTable
WHERE Column1 NOT LIKE '% %'
Or:
WHERE LOCATE(' ',Column1) = 0
Related
I have a MySql database with some data like this:
str = '554a41a6e4d9d 677 3'
and i want to search all the values that exactly match the '677' part (the second part of text, after the first).
How i can accomplish this?
If the 'parts' are delimited by space, you can use like:
SELECT [cols] FROM [table] WHERE [col] LIKE '% 677 %'
You should use Fulltext type for the column. And then use a query like this:
SELECT * FROM table WHERE MATCH (column) AGAINST ('677');
I have in table column pnum_s
I need get only that rows, which value in column pnum_s is exactly 10 symbol and all these symbols are only digits
what query must write for this?
I am trying
SELECT * FROM mytable WHERE pnum_s REGEXP '^\d+$'
But this not returns 0 rows
The pattern you are looking for is either '^[0-9]{10}$' or '^[[:digit:]]{10}$'.
Everything is in the manual.
I think with Mysql you'll want something like this:
^[[:digit:]]{10}$
Check out the reference page.
http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp
What you're looking for is this:
SELECT * FROM mytable WHERE pnum_s REGEXP '^[[:digit:]]{10}$';
try this pattern:
'^[0-9]{10}$'
I want to use LIKE query to select some specific word just like UU1.
I use
SELECT UniID FROM MasterCustomer where (UniID like '%uu1%' )
But it also returns a row which contains UU11.
How can I modify my query to select only UU1?
My table contains values like
UU1-UU7-UU5
UU2-UU1
UU3-UU1
UU31-UU14
try this:
SELECT UniID
FROM MasterCustomer
WHERE (UniID like '%uu1' ) OR (UniID like '%uu1-%' )
like '%uu1-%' or like '%uu1 %'
Another alternative:
WHERE UnitID + '-' LIKE '%uu1-%'
Can't seem to find documentation on a particular formation of SQL using the LIKE operator. Using MySQL, a typical query for multiple words using the LIKE operator may look like this:
SELECT * from table AS t WHERE t.col LIKE '%word1%' AND t.col LIKE '%word2%'
Although the following statement also works, the rows returned will vary depending on the order of the words in the query. For example:
SELECT * from table WHERE col LIKE '%word1%' '%word2%'
executes without the AND boolean, but with different results from:
SELECT * from table WHERE col LIKE '%word2%' '%word1%'
My question is, what is actually happening when using this formation of the query instead of using boolean?
From the manual:
Quoted strings placed next to each other are concatenated to a single
string. The following lines are equivalent:
'a string'
'a' ' ' 'string'
So, what's happening is that '%word1%' '%word2%' is being interpreted as '%word1%%word2%'
If someone passes a '%' to a field that compares in my sql with su.username LIKE CONCAT('%', email ,'%')) it returns all rows. It ends up looking like su.username LIKE CONCAT('%%%'). Can I get around this in anyway without filtering out the '%'?
I'm assuming you mean you want to escape the % so it matches a literal % instead of anything.
In that case, you just need:
... su.username LIKE CONCAT('%',REPLACE(email,'%','\\%'),'%')
You need to escape the %, so it literally matches '%'
select * from mytable
where mycol like '%\%%';