I observed that this query returns all values from the database.
SELECT * FROM projround3.add_user where user_email like '%' '%';
Why is that?
This query:
SELECT *
FROM projround3.add_user
WHERE user_email like '%' '%';
Would not be valid in most databases. Some interfaces concatenate adjacent strings, so this is interpreted as:
SELECT *
FROM projround3.add_user
WHERE user_email like '%%';
The two wildcards are the same as one. This will match every non-NULL value.
If you want to find emails with a space, then correct logic is:
SELECT *
FROM projround3.add_user
WHERE user_email like '% %';
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.
I'm trying to list all users beginning with a letter, e.g. D
Would the following be the right method of doing this.
Select concat(firstname, '',lastname) from users where concat(lastname) = "D*"
SELECT concat(firstname, '',lastname) FROM users WHERE lastname LIKE "D%"
If you want to use wildcards, you need the LIKE operator. Also, in your where clause you only have one column (lastname), so you don't need concat.
i would try:
select * from users where lastname like 'D%';
For getting list starting with eg: "D":
SELECT firstname FROM users WHERE LEFT(firstname,1)= 'D';
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.