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%'
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
I have name,surname,city,address,mobile fields in mysql table.
I want a select statement that select all the records if any of the above fields data matches;
For eg: If i put name: yusuf. then select statement should look data :yusuf in all column mentioned above and if any of the record matches then it should show the result.
Lets say you search by name.
Then use SELECT * FROM table WHERE name LIKE userinput.
Hope this help
select name,surname,city,address,mobile from Table
where name like '%Yusuf%'
Or Surname like '%Yusuf%'
or City like '%Yusuf%'
Or Address like '%Yusuf%'
Or Mobile like '%Yusuf%'
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'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'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