How do I do full text indexing on 2 MySQL columns? - mysql

I have 2 columns: first_name, last_name. That's it.
When a user searches for a name, I want both columns to act as one. (as a full name).
So when someone searches for "Alex Johnson", I want it to query both columns as if they were one column.

Will something like this help:
ALTER TABLE your_table_name ADD FULLTEXT(first_name, last_name);

Try to use concat in your query.
like:
select conact(first_name, last_name) as fullname from ...
detailed description you will find here:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat

You can concat the columns together to search on them such that a simply query would look like this:
create temporary table test(n1 varchar(10), n2 varchar(10));
insert into test values('tommy','bobby');
select * from test where n1+n2='tommybobby';
+--------+--------+
| n1 | n2 |
+--------+--------+
| tommy | bobby |
+--------+--------+
Full more advanced Full Text searching it sounds like you want to use the Match function.
SELECT n1, n2 FROM test
WHERE MATCH (n1,n2)
AGAINST ('+tommy +bobby' IN BOOLEAN MODE);
See MySQL FULLTEXT Indexing and Searching for more info.
*edited because I forgot about the minimum word size.

Related

Mysql Insert Select Common Phrase into new table

I have a Table which with many rows and looks like something below.
id Name
1481 Carrier_186_CLI
1469 Carrier_186_OUT
1480 Carrier_107_CLI
1483 Carrier_107_OUT
All i want is to copy the rows into another table which has the common phrase , like
id Name
1 Carrier_186
2 Carrier_107
I tried this
Insert into newtable(Name)select Name from oldtable group by Name
I know it wont work but i am unable to find a solution for this. I am new to mysql.
You can use string function substring_index to pick value before 2nd underscore
Insert into newtable(Name)
select substring_index(Name,'_',2)
from oldtable
group by substring_index(Name,'_',2)
Demo

MySQL using like to match specific string

In my column I can have either a string like : "data+" or "data+data+data+..(undefined times)..+"
I simply need to get the column where I have multiples data and not only one.
I tried with
mycol NOT LIKE '%+'
But it didn't work...
Actually I don't know the data it is a string that varies : 'blabla' or 'aString' or 'whatever'
If I had in my columns
'jdsjpgsg+jdsjpgsg+', 'dvgff+', 'ffef+eefds+ghghgh+'
I want to select only
'jdsjpgsg+jdsjpgsg+',
'ffef+eefds+ghghgh+',
NOT 'dvgff+' !
if you want to search '..xxx+..' then you should be use xxx+%
if you want to search '..+xxx..' then you should be use %+xxx
if you want to search '..++..' then you should be use %++%
if you want to search '..+..+..' then you should be use %+%+%
It is what I get too and I dont want that. It is actually what i don't want to select. If I had jdsjpgsg+jdsjpgsg+ in my table I want to select it and NOT jdsjpgsg+ It is tricky...
so you can try like '%+%+%' to exclude just one '+'
CREATE TABLE TestTable
(`text` varchar(90))
;
INSERT INTO TestTable
(`text`)
VALUES
('jdsjpgsg+jdsjpgsg+'),
('dvgff+'),
('ffef+eefds+ghghgh+')
;
select * from TestTable
where text like '%+%+%'
| text |
|--------------------|
| jdsjpgsg+jdsjpgsg+ |
| ffef+eefds+ghghgh+ |
SQL Fiddle Demo Link
The % is the wildcard character. You should be using the % after data. Try like:
SELECT * FROM `table` WHERE `mycol` NOT LIKE 'data+%'
The above query will filter out all the records that have any characters after data+.

select a column value that matches with a value of another column

I have a MySQL table called 'Employee'. It has seven columns, but only two column values are related to my question. The two column names are FullName and Name.
Here are some sample values of the two columns in the table for better understanding.
FullName Name
----------
MichealPhilips | Philips
Louisfarak | louis
Waynebruce | kirten
I want to find the rows where FullName value contains the value of name. So in my example the answer should be MichealPhilips and Louisfarak but not Wayne bruce because FullName(Waynebruce) does not contain Name(Kirten).
I have tried a query some thing like this:
SELECT * FROM Employee WHERE FullName LIKE '%' || Name || '%';
But it seems like a wrong query. It is printing all the rows in the table and I don't know why.
Can someone please help me in this? Are there any other ways to write this query? Is my query wrong?
This should do it for you...
SELECT
a.FullName
FROM
Employee a
WHERE
a.FullName LIKE CONCAT('%', a.Name, '%')

MySQL How to Select only columns where value contains string

I did some searching and from one question already posted on stackexchange, the answer was that it was not possible, but I figured to ask. I did not know if it was possible to form a SELECT query to dynamically select which columns will be displayed in a mysql SELECT statement result. Example:
Say I have column names Person, ID, Phone Number, Alt Number for this table:
John | 79 | 800-499-0000 | 800-499-5555
I would like to form a SELECT statement so that it will only pull down columns where string '800-499' is somewhere in the field. Thus the result from MySQL ideally would be:
800-499-0000 | 800-499-5555
The only problem is that I do not think dynamically selecting columns is possible.
Any help or confirmation is appreciated.
You could try something like:
select * from
(select concat(case when col1 like '%800-499-0000%' then concat('col1: ',col1,';') end,
case when col2 like '%800-499-0000%' then concat('col2: ',col1,';') end,
...
case when coln like '%800-499-0000%' then concat('coln: ',coln,';') end)
as search_results
from my_table) sq
where search_results is not null

mysql select query - to match one field with the other field

I am trying to find records that has the following scenario.
ID | name | email
1 Robert robert#gmail.com
2 William bill#gmail.com
3 Michael michael#gmail.com
4 Micahel mike#gmail.com
Based on the above table, I want to find the records where the "name" is contained in the "email field", here record 1 and 3 should be the output and not 2 and 4. Is there any way I can do this comparison?
I tried reading about regex but couldn't find anything. If it's comparison of same value, it will be straightforward, but I am not having any clue for this one. I thought of LIKE but looks like this cannot have field names.
The exact syntax will depend on how you want to define the relationship.
Are you looking for the name anywhere in the email address? (This will be slow)
select id,name,email
from your_table
where email like concat('%',name,'%')
Just at the beginning of the email address?
select id,name,email
from your_table
where email like concat(name,'%')
Just before the # sign?
select id,name,email
from your_table
where email like concat(name,'#%')
You can use LIKE, you just have to use it in combination with CONCAT.
SELECT
ID,
name,
email
FROM
yourTable
WHERE
email LIKE CONCAT(name, '%');
The CONCAT will return a string which can be used to match against email via LIKE.
This should work
SELECT * FROM table WHERE email LIKE (CONCAT('%',name,'%'))
select * from your_table where lower(substring_index(email,'#',1))=lower(name)