i have two tables: songs and singers
and this my query:
Select * FROM (
SELECT
idx,
name,
IDsinger,
permission,
(name LIKE '%XXX%') As relevancy,
'table1' As t
FROM `songs`
where
isActive<>'0' AND name LIKE '%XXX%'
UNION
SELECT
idx,
name,
CreationDate,
permission,
(name LIKE '%XXX%') As relevancy,
'table2' As t
FROM `singers`
WHERE isActive<>'0' AND name LIKE '%XXX%'
) AS X
order by relevancy LIMIT 10
The problem is if i write "akon lonely" is not found result.
But if i write "akon" or "lonely" is found result.
And i would love suggestions for improving query..
Thanks
Your result is correct. AKON is a singer and Lonely is the song. Neither the Song or the Singer is called Akon lonely.
Sounds like you are trying to do a more complicated form of searching like contains.
http://technet.microsoft.com/en-us/library/ms187787.aspx
You could try
name like '%' + #xxx + '%' or #xxx like '%' + name + '%'
Related
I am writing a query where I am trying to pull out values based on a portion of a subquery and can't seem to work out how to get the needed values.
The table has only a half-dozen entries containing author names but in some instances, there is more than one author in the field and I need to get their separate individual biographies. Right now this gives no errors but it is pulling up only the single entry that contains both author names rather than the two that contain the actual biographies. I need only the two single-name entries, not the one containing both but it's giving the opposite.
AuthorName has something like this:
Joe Blow
Jane Doe
Jow Blow and Jane Doe
and here is the query
SELECT a.`ID` AS AuthorID, `AuthorName`, `AuthorPhoto`, `AuthorBio`, `Email`,
FROM authorbiographies a
WHERE `AuthorName` LIKE CONCAT('%',(
SELECT `AuthorName` )
FROM authorbiographies WHERE `AuthorName` LIKE '% and %),'%'
)
AND `ID` <> 3
This can be solved by joining a derived tables of all authornames that don't include and with a derived table of all authornames that do include and, as long as the first authorname is part of the second:
SELECT a1.id, a1.authorname, a1.authorbio
FROM (SELECT *
FROM authorbiographies
WHERE authorname NOT LIKE '% and %'
) a1
JOIN (SELECT *
FROM authorbiographies
WHERE authorname like '% and %'
) a2 on a2.authorname LIKE concat('%', a1.authorname, '%')
Output (for your sample)
id authorname authorbio
1 Joe Blow has lived a long time
2 Jane Doe wrote several books
Demo on SQLFiddle
Doesn't this do what you want?
SELECT ab.*
FROM authorbiographies ab
WHERE ab.authorname NOT LIKE '% and %';
You seem to just want to pick out the rows that do not have ' and ' in them.
How to write a query to retrieve data, only with specific last name first letter.
For example I need a query to retrieve last name starting with A, B, C and E.
Use LIKE and %:
SELECT * FROM people WHERE last_name LIKE 'A%' OR last_name LIKE 'B%' OR last_name LIKE 'C%' OR last_name LIKE 'E%'
Thanks a lot. With the model you have provided me with I have develop the one below:
SELECT vendor_name,
CONCAT(vendor_contact_last_name, ', ', vendor_contact_first_name) AS full_name
FROM vendors
WHERE vendor_contact_last_name LIKE 'A%' OR vendor_contact_last_name LIKE 'B%'
OR vendor_contact_last_name LIKE 'C%' OR vendor_contact_last_name LIKE 'E%'
ORDER BY vendor_contact_last_name;
Below SQL works for most of the cases
SELECT * FROM people WHERE last_name LIKE '[A-E]%';
The brackets mean anything from alphabets A to E and the percentage (%) means followed by any number of any characters.
If your running SQLite, you can try the below SQL
SELECT * FROM people WHERE last_name >= 'A' and <= 'E';
Hope this helps.
I am trying to write a select statement that uses several keywords to search by. For example
SELECT id FROM my_mod mm WHERE (mm.name LIKE '%joe%' OR mm.name LIKE '%jim%');
What I would like to do is in the return data have a extra column that counts the number of times that row matches one of the like statements.
So for the one above if there is a name jimjoe it would have a count of 2 or if the name is jim it would have a count of 1. Does anyone know of a way to do this?
I should also mention that I can't use temp tables.
SELECT id,
CASE
WHEN mm.name LIKE '%joe%'
AND mm.name LIKE '%jim%' THEN 2
ELSE 1
end AS cnt
FROM my_mod mm
WHERE mm.name LIKE '%joe%'
OR mm.name LIKE '%jim%'
Try this query -
SELECT
COUNT(IF(name LIKE '%joe%', 1, NULL)) joe_count,
COUNT(IF(name LIKE '%jim%', 1, NULL)) jim_count
FROM
my_mod
WHERE
name LIKE '%joe%' OR name LIKE '%jim%'
Try this
select t.id,count(*) FROM
((select id from my_mod where name like '%jim%')
union all
(select id from my_mod where name like '%joe%')) t group by t.
I check here how it works
I have a table with 3 fields, name, firstname and lastname
I want to see how many rows in the table have name of the form firstname%lastname
I tried to do
select * from family_watchdog_offender where name like firstname%lastname\G
but that returned a syntax error regarding the %lastname portion of the query. Is there some syntax that will allow me to run a query such as this?
SELECT * FROM family_watchdog_offender WHERE name LIKE CONCAT(firstname, '%', lastname);
Try concat-ing the %:
select * from family_watchdog_offender where name like CONCAT(firstname, '%', lastname)
I don't think you can do that...This may be what you need to do
SELECT *
FROM family_watchdog_offender
WHERE name LIKE CONCAT(firstname, '%')
AND name LIKE CONCAT('%', lastname);
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.