I have a table with many fields out of which LastName and FirstName are two..
This table has around 2 million records..
So I thought of using Full text search.
Now my requirement is to search for a particular record with search criteria LastName and FirstName with combination of LastName and FirstName in the database table
..|Firstname |Lastname |..
------------------------------------------
|george walker|bush |
|bill |klinton |
It should return the record if the combination of these two columns contains the extered search criteria
ie., FirstName=bush and LastName=walker or
FirstName=goerge and LastName=walker etc.
I cannot just use like
SELECT * FROM TableName where CONTAINS((LastName,FirstName),#LastName)
and CONTAINS((LastName,FirstName),#FirstName)
because searching criteria FirstName and LastName can be null(SQL SERVER 2008 raises Error) or can have more than one word in it
How to solve the problem?
SELECT * FROM TableName where FirstName IS NOT NULL AND LastName IS NOT NULL (FirstName LIKE '%BUSH%' OR FirstName LIKE '%george%') AND LastName LIKE '%walker%'
Try that
Did you want to allow or disallow nulls from results?
Related
I have been asked to fetch data from Mysql-Table where Firstname and Lastname column contains similiar or alike values.
So far i have tried to use soundex expression
select * from table where soundex(firstname) = soundex(lastname)
but there are many which do not have any similarity and there are many which hasn't been found. So the gathered results are not really satisfactorily.
I have to mention, that our lastname and firstname values are mostly strong international..
Anyway, the question is: Is there an simple approach to find the rows without crafting around?
U could try:
SELECT * FROM table WHERE lastname LIKE ('%' || firstname || '%') OR firstname LIKE '%' || lastname || '%'
or something alike
SELECT *
FROM customers
WHERE Firstname LIKE 'George'
The problem is that i have more than 1 rows in the table with tha name Geoge and the result of the query shows only one row
You will want to include the wildcard % character to include the rows the have George present in the name:
SELECT *
FROM customers
WHERE Firstname LIKE '%George%';
If George will always appear at the beginning, then you can include the wildcard on the end:
SELECT *
FROM customers
WHERE Firstname LIKE 'George%';
you need to add a wildcard character % to match any value that contains george
SELECT *
FROM customers
WHERE Firstname LIKE '%George%'
MySQL LIKE Operator
the statement
WHERE Firstname LIKE 'George'
is equivalent with
WHERE Firstname = 'George'
that is why you are only getting one record which firstname is george.
UPDATE 1
SQLFiddle Demo
try
LOWER(Firstname) LIKE '%george%'
handles partial values and avoids case sensietivity issues.
I was wondering if it was possible to override column value in the Where clause of a SQL query (MySQL in my case).
To be more clear, here is an example :
Suppose a basic query is :
SELECT lastname, firstname FROM contacts WHERE lastname = "Doe";
Is it possible to force lastname and firstname to return value from an other table, just by modifying what is after the WHERE part ? Something like
SELECT lastname, firstname FROM contacts WHERE lastname = (SELECT name FROM companies);
I am currently testing a web application, and I found a SQL Injection flaw where I can change Doe to whatever I want, but I'm limited with only one query (mysql_query restriction of PHP) and addslashes (so no " and ').
possible could be
SELECT lastname, firstname FROM contacts WHERE lastname = "{0}" UNION SELECT {1} --
where {0} non existed value and {1} data from other tables
UPDATE from wiki example
$res = mysql_query("SELECT author FROM news WHERE id=" . $_REQUEST['id'] ." AND author LIKE ('a%')");
become
SELECT author FROM news WHERE id=-1 UNION SELECT password FROM admin/* AND author LIKE ('a%')
The syntax that you used in your SELECT ... WHERE clause is a standard SQL feature called a subquery.
In the context of your example there is a restriction on the subquery to return just single value. Otherwise your query is a valid SQL and you can change subquery to return multiple values (with implicit OR) using IN operator like this:
SELECT lastname, firstname FROM contacts
WHERE lastname IN (
SELECT name FROM companies
);
You can dig deeper into this subject to uncover correlated subquery.
In a database i have a table prospect and has two columns firstname and lastname.
Now the issue is that i want to search in both columns; the easy solution would be to use a query like
SELECT * FROM `prospect` WHERE lastname like '%piece of lastname%' or firstname like '%piece of firstname%'
This however requires to have two search fields, firstname and lastname. I want that users can search in one field. How should a query look like when I want to achieve this?
Do you mean you want to search the concatenation of two fields? Then you can use something like:
SELECT * FROM prospect
WHERE CONCAT(firstname,' ',lastname) LIKE '%ohn Smit%'
Is this is what you are looking for?
SELECT * FROM prospect
WHERE firstname + ' '+ lastname LIKE '%name%'
I'm new to database and querying in MySql. I have a table in my database with id, fistname, middlename and lastname. In my webapp, I have a search box so that I can search my database using the firstname, middlename and lastname and it will display the result in my web app. Do I need to use SELECT statement to do this and what condition should I query?
Please advise.
Many thanks.
select id, firstname, middlename, lastname where firstname like '%searchterm%' or middlename like '%searchterm%' or lastname like '%searchterm%'
this will give you a very broad result, as it matches on the search term anywhere within any of the name fields.
you can also use 'searchterm%' to match ones starting with the searchteam, and 'searchterm' for an exact match