Query to check if a certain row has 2 words - mysql

How to return rows where a column has 2 words (that is, strings separated by a space) in it?
It must be purely using SQL.
SELECT * FROM table WHERE name (has 2 strings in it);

I dont know the names when querying. Its a big dataset. I only have to check if the name contains a spacebar basically (from a comment).
If you want to distinguish names that have two parts from one-part and three-plus-part names, you can use regular expression:
SELECT * FROM my_table WHERE name REGEXP '^[^ ]+[ ]+[^ ]+$'
This regular expression matches when the entire string consists of two non-empty parts containing no spaces, with one or more space separating them.

This perfectly works for me
You can use 'AND' condition and Like Operator with wildcards (%).
SELECT * FROM table_name WHERE name LIKE '%Word1%' AND name LIKE '%Word2%'

How about simply:
...
WHERE [name] LIKE '%Word1%'
AND [name] LIKE '%Word2%'

SELECT * FROM table WHERE concat(' ',name,' ') like '% str1 %'
AND concat(' ',name,' ') like '% str2 %'
The extra blanks are there to separate words.

You can use the following technique
mysql> select length('first name')-length(replace('first name',' ','')) as diff;
+------+
| diff |
+------+
| 1 |
+------+
i.e. get the difference of actual name and the name after replacing space, and if its 1 then you have the value as firstname lastname
So the query may look like
select * from table
where
length(col_name)-length(replace(col_name,' ','')) = 1

Use % between words.
Example:
SELECT * FROM Table WHERE Col LIKE '%word1%word2%'

SELECT * FROM table_name WHERE name LIKE '% %'

Related

sql select where string matches pattern defined in another table

I have a select statement like this:
Select * from A where name like 'a%' or name like 'b%' or name like 'j%' or name like ... etc
Is it possible to store a%, b%, j% in a table somewhere so I can more easily manage this list and convert my query to something like:
Select * from A where name like (Select * from StringPatternToMatch)
Try this:
SELECT * FROM A
JOIN StringPatternToMatch patt ON A.name LIKE '%' + patt.pattern + '%';
Replace patt.pattern with the name of the column in your StringPatternToMatch
You can do a regexp search instead.
select *
from A where name regexp '^[abjf]';
It's easier query to maintain than a ton of or'd likes.
demo here
'^[abjf]' means match the start of the string (^), followed by any of the characters in the list ([abjf]). It doesn't care what comes after that.
Just add more letters to the list if you find names starting with them.

MySQL SELECT values with more than three words

I currently have the following code
select *
FROM list
WHERE name LIKE '___'
ORDER BY name;
I am trying to get it so that it only shows names with three or more words.
It only displays names with three characters
I cannot seem to work out the correct syntax for this.
Any help is appreciated
Thankyou
If you assume that there are no double spaces, you can do:
WHERE name like '% % %'
To get names with three or more words.
If you can have double spaces (or other punctuation), then you are likely to want a regular expression. Something like:
WHERE name REGEXP '^[^ ]+[ ]+[^ ]+.*$'
you can count number of words and then select those who are equal or greater then 3 words.
SELECT * FROM list
HAVING LENGTH(name) - LENGTH(REPLACE(name, ' ', ''))+1 >= 3
ORDER BY name
DEMO HERE
*Even if you have multi spaces it will not affect here check this
I do believe what you are after is
SELECT *
FROM list
WHERE name LIKE '% % %'
ORDER BY name;

how to know the count of words starting with a particular word in database?

I am using a mysql database for storing the data. The table consists of 2 columns s_no and name. I used index on this table on name. Now I want to get only words starting with a particular letter say "a" or "b" etc. How can I do it? Is there any SQL query for retrieving data in this fashion?
select s_no,name from tablename where name like 'a%' or name like 'b%'
select * from table_name
where name like 'a%'
select s_no,name from table where name like 'a%' or name like 'b%'
Select * from tablename where name like 'a%'
You can also pass the value inside parameters
#value varchar(50)
Select * from tablename where name like #value
so whatever characters u insert like a or b just mearge it with % and here you go
like #value=#value+ '%' before query .

Compare comma-separated string in MySQL column where column is also comma separated

Compare comma-separated string in MySQL column where column is also comma separated
For Example:
Id name catid
1 abc 4,5,2
2 bcd 5
3 efg 9,1,7
SELECT * FROM TABLE WHERE catid IN (2,5,6)
Here 2,5,6 I have to compare with the catid value to get the result.
Any Help to get the right out put, I used FIND_IN_SET, but could not make it work for my case
according to your problem you can use LIKE operator instead of IN
SELECT * FROM testchintan WHERE CONCAT(',',catid,',') LIKE '%,5,%'
OR CONCAT(',',catid,',') LIKE '%,2,%' OR CONCAT(',',catid,',') LIKE '%,6,%'
OR YOU can use REGEXP
SELECT *
FROM testchintan
WHERE catid REGEXP '[4,9,5]'
OR
SELECT *
FROM testchintan
WHERE
REPLACE(catid,',','|') REGEXP '[9,7]'
Note that I'm not seriously advocating this as a solution...
SELECT * FROM my_table WHERE FIND_IN_SET(5,catid) OR FIND_IN_SET(2,catid);

Select the first word from a column that may have more words

Lets say I have a table simple as this:
id | name
1 | one_word
2 | two words
3 | here we have four
So, I would like to get only rows containing one word, which in the above example would only be record with id 2.
I did read the docs and tried various versions of this:
SELECT * FROM `table` WHERE name REGEXP '(.*?)\s';
so, please tell me where I'm doing it wrong.
If you are looking for the first word from a column that may have more words, you can use SUBSTRING_INDEX:
SELECT SUBSTRING_INDEX("here we have four", " ", 1)
to extract rows that don't have spaces, you can use for example this:
name NOT REGEXP '\s'
name NOT LIKE '% %'
LOCATE(' ', name) = 0
if your column can contain spaces at the beginning or at the end of the string, you could also use TRIM:
SELECT SUBSTRING_INDEX(TRIM(" here we have four "), " ", 1)
TRIM(name) NOT REGEXP '\s'
TRIM(name) NOT LIKE '% %'
LOCATE(' ', TRIM(name)) = 0
Thx to the comment from Strawberry, I came up with this:
SELECT * FROM `table` WHERE name NOT REGEXP '\s'
which works great.
What about this? In both LIKE '% %' and REGEXP case performance is bad. But i think LIKE has the least bad performance.
SELECT * FROM `table` WHERE name NOT LIKE '% %'