How to query a first name and last name string in MySql - mysql

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

Related

Replace function in mysql not working

I have a table like this;
Lastname
MORALES
THOMPSON
SMITH
but I want to use the replace function to change all the lastname to another character like this:
I want 'MORALES' to be replace with 'TEYE'
I tried this syntax;
select lastname, REPLACE(lastname, 'M', 'TEYE')
from customers;
but this is what i'm getting;
'MORALES TEYEORALES'
Instead of
'MORALES TEYE'
please I need help.
Thanks
You told it only to replace M, not the whole name, that's what it did. If you want to replace the whole name, write:
SELECT lastname, REPLACE(lastname, 'MORALES', 'TEYE')
FROM customers
REPLACE() replaces substrings, so if there's a lastname = AMORALES, the result will be ATEYES. If you only want to replace it when it's the entire name, you can use:
SELECT lastname, IF(lastname = 'MORALES', 'TEYE', lastname)
FROM customers
not an expert in mySQL; but, it looks like you are getting what you asked for, the last name folowed by the last name where the "M" was replaced with "TEYE".
have you tried appending the string you want added?
select lastname + " TEYE" (or some variation to concatenate in mySQL syntax.
if the last name only needs to be update if it currently equals "MORALES", you can most likely add a where (row selection) clause (something like where lastName = "morales") to filter the rows being updated

Override column value in WHERE clause?

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.

Sql Server Full Text Search - LastName FirstName searching prob

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?

SQL query to search in a concated string

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%'

How to use like function in MySQL or run LIKE command in mysql when search need to more specific

I want to search in database that how much person have name 'i am steven'
I want to search on user table with their three column
firstname
secondname
lastname
I want to sorting them as First name match comes first secondname latter and lastname last when I see the result.
are their any command in mysql solve this puzzle
select
if (firstname like 'i am steven%', 4,
if(secondname like 'i am steven%', 2,
if (lastname like 'i am steven%', 1, 0)
)
) as first_name_first
from
user
where
firstname like 'i am steven%' or
secondname like 'i am steven%' or
lastname like 'i am steven%'
order by first_name_first desc;