In my table I store first name in the fName column and last name in the lName column, now I need to search them with a query, but I don't know the SQL!
example
lName | fName
-----------------
Tendulkar| Sachin
Ganguly | Sourav
Khan | Zaheer
Dhoni | Mahendra Singh
The user should get MAHENDRA SINGH DHONI if he searches for Mahendra Dhoni!
select concat(fName,' ',lName) fullname
from tbl
where concat(' ',fName,' ',lName,' ') like '% Mahendra %'
and concat(' ',fName,' ',lName,' ') like '% dhoni %'
This will most certainly put to rest any hopes of a well performing query
A variation on the theme
select concat(fName,' ',lName) fullname
from tbl
where (concat(fName,' ',lName) like '%Mahendra%dhoni%'
or concat(lName,' ',fName) like '%Mahendra%dhoni%')
The 2nd version doesn't care about full part matching, e.g. dhoni will match madhonie
Both of these queries find the name correctly. Note that there are % before and after the name to match, as well as % for every space in the name.
create table tbl (fname varchar(100), lname varchar(100));
insert tbl select 'Mahendra singh', 'dhoni';
select concat(fName,' ',lName) fullname
from tbl
where (concat(fName,' ',lName) like '%Mahendra%dhoni%'
or concat(lName,' ',fName) like '%Mahendra%dhoni%');
select concat(fName,' ',lName) fullname
from tbl
where (concat(fName,' ',lName) like '%dhoni%Mahendra%'
or concat(lName,' ',fName) like '%dhoni%Mahendra%');
You are not clear on the nature of the search inputs and specifically the level of flexibility. First, is the user given two boxes for first and last name or only a single search box? If the former, then the fast solution would be:
Select concat( fname, ' ', lname)
From MyTable
Where lname Like 'dhoni%'
And fname Like 'mahendra%'
The above query only searches for where the first part of the column value begins with the search values. However, if the user can type anything into a single search box, that is harder. If it is presumed that the user has typed <name part> space <name part>, then one solution that solves that specific problem where the user enters only two words is to split on the space and run something like:
Select concat( fname, ' ', lname)
From MyTable
Where ( lname Like '%dhoni%' And fname Like '%mahendra%' )
Or ( lname Like '%mahendra%' And fname Like '%dhoni%' )
However, that query will perform awful because it forces the system to scan the entire table each time it is executed. Further, what happens when they enter a three part name like Mahendra Singh Dhoni in your search? There are simply too many edge cases for this to be workable IMO. The right solution is to get a full text indexing engine like Lucene that will create a index across both columns and rank the quality of the match.
Lucene
I'm guessing your inputs are from two different textboxes, and hopefully you are using a stored procedure :).
declare #input1 nvarchar(50)
declare #input2 nvarchar(5)
set #input1 = 'Mahendra'
set #input2 = 'Dhoni'
select upper((fname + ' ' + lname)) as 'FullName'
from customer
where fname like '%' + #input1 + '%'
or fname like '%' + #input1 + '%'
or lname like '%' + #input2 + '%'
or lname like '%' + #input2 + '%'
If you have 1 input box, you will want to split your search term by the space and loop over your search terms against fname and lname columns using the like '%term%' syntax.
Another way would be to make a stored procedure that added fname and lname together and did a soundex match. this is where you match your search term against the sound of the word in the table. Google it should help, its pretty easy.
I am assuming you have 2 inputs and you are only getting part of the first name. If this is true then the following would work:
select concat(fname, ' ', lname)
from yourtable
where substring(fname, 1, 5) = 'mahen'
and lname = 'dhoni'
The advantage of this approach is that it will use the indexes on the column(s) or a combined index whereas the like queries always do full table scans.
You might also check out a search engine like sphinx or solr when you truly do not know the data you are receiving as those are far more flexible that database queries.
Related
I'm having an issue in searching last name in full name. It is working with %% but I don't what to use it because all has 'A' are showing. Is there any alternative way on search last name? For example I have a full name John Doe and I only searched Doe, how to get the result even if I searched the last name?
SELECT * FROM Customers where CustomerName LIKE '%Doe%' - this is working
Select * from table_name where Column_Name Like "%last_name"
that will select only full names ends by last_name
Here, I am shown you. Please, try this both of one.
SELECT FullName FROM dbo.TableA where FullName LIKE '%Choksi%'
SELECT SUBSTRING(FullName, CHARINDEX(' ', FullName) + 1, LEN(FullName)) AS [Last Name]
from dbo.TableA
where FullName like '%Choksi%'
I want to achieve is to search for a full name in a database with only a string with his first and last name.
in database there is the full name:
Manuel Augusto Vieira Conde Fialho
and I want a sql query that can search this full name only with the first and last name :
Manuel Fialho
I've tried this one:
SELECT name FROM partners WHERE name LIKE 'Manuel Fialho'";
but this does not work for me because I don't have any Manuel Fialho in the database only a Manuel Augusto Vieira Conde Fialho and because of that the name im looking for does not show.
thank you
You shoould add %:
SELECT name FROM partners WHERE name LIKE 'Manuel%Fialho';
One method uses wildcards:
SELECT name
FROM partners
WHERE name LIKE REPLACE('Manuel Fialho', ' ', '%');
SELECT name FROM partners WHERE name LIKE "Manuel%" OR name LIKE "%Fialho";
If You are sure about the last name of the name then you can also use AND
SELECT name FROM partners WHERE name LIKE "Manuel%" AND name LIKE "%Fialho";
There are 2 possible options to search.
Split search string into multiple words and query with that many LIKE patterns.
select name FROM partners WHERE name LIKE '%Manuel%' or name like '%Fialho%'
If you are sure that the sequence of words in the search strings are to match with those in the column value then replace all spaces with % symbol and use with LIKE.
select name FROM partners
WHERE name LIKE concat( '%', replace( 'Manuel Fialho', ' ', '%' ), '%' )
Try Below Query:
It works fine..!
SELECT * FROM `partners` WHERE
`name` LIKE replace( 'Manuel Fialho', ' ', '%' ) OR
`name` LIKE replace('%Manuel Fialho%', ' ', '%' ) OR
`name` LIKE replace('%Manuel Fialho', ' ', '' ) OR
`name` LIKE replace('%Manuel Fialho%', ' ', '' );
I have a NAME column and the data is stored like this in a MySql database:
Doe,John P
I would like to break it out into FNAME LNAME MNAME and FULLNAME.
If I don't want to tamper with the SQL table, should I create a view?
I know how to fix it in PHP with a function but I would rather the processing be done on the database server than the application server.
This is what I have so far. I thinking I need a compound query or something similar.
SELECT Name
, SUBSTRING_INDEX(Name,',',-1) As FNAME
, SUBSTRING_INDEX(Name,',',1) As LNAME
, SUBSTRING_INDEX(Name,' ',1) As MNAME
FROM people;
I'm having trouble grabbing the middle name and doing what I think should be CONCAT function to get FULLNAME.
It would be great if there was a way (maybe a conditional) to handle names in the database that didn't have middle names. (i.e. DOE,JOHN).
A view is a fine way to get the results you want for future queries. The defining query would be:
SELECT Name, SUBSTRING_INDEX(SUBSTRING_INDEX(Name, ',', -1), ' ', 1) As FNAME,
SUBSTRING_INDEX(Name, ',', 1) As LNAME,
SUBSTRING_INDEX(Name, ' ', -1) As MNAME
FROM people;
This will work assuming that the format is exactly correct as you describe it in the table. Small changes -- such as no middle name or a space after the comma -- would make the syntax more complicated.
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 '% %'
So the search would be like 'Ozzie Smith'
First table has (username, fname, lname) = osmith, ozzie, smith
Second table has (username, team) = osmith, cardinals
I need to concat the first and last name columns from the first table join it by username on the second table and return the team name.
I have been trying and trying...brain melted. I need your help!
Thanks!
Since it's MySQL, you need to use CONCAT, not the + sign. I also added a LOWER() function to avoid capital letter mismatch problem :
select team
from table1
join table2 on table2.username = table1.username
where lower(concat(fname,' ',lname)) like lower('%Ozzie Smith%')
You're probably doing something like
WHERE fname LIKE '%Ozzie Smith%'
which will not work. fname will contain only Ozzie, and will never match against Ozzie Smith. It would also not match if the user enters Smith, Ozzie, or any other dual-name variant. You'll have to preprocess the search terms and do something like
WHERE (fname LIKE '%Ozzie%') or (fname LIKE '%Smith%')
OR (lname LIKE '%ozzie%') or (lname LIKE %Smith%')
This will get VERY painful as the number of search terms and fields being search goes up. You'd be better off using a FULLTEXT index, where it'll be a simple matter of
WHERE MATCH(fname, lname) AGAINST ('Ozzie Smith')
Why doesn't this work?
select
lname + ', ' + fname as playerName
, team as teamName
from table1
join table2 on table2.username = table1.username
Update:
where (fname + ' ' + lname) like '%Ozzie Smith%'