I have a MySQL table called 'Employee'. It has seven columns, but only two column values are related to my question. The two column names are FullName and Name.
Here are some sample values of the two columns in the table for better understanding.
FullName Name
----------
MichealPhilips | Philips
Louisfarak | louis
Waynebruce | kirten
I want to find the rows where FullName value contains the value of name. So in my example the answer should be MichealPhilips and Louisfarak but not Wayne bruce because FullName(Waynebruce) does not contain Name(Kirten).
I have tried a query some thing like this:
SELECT * FROM Employee WHERE FullName LIKE '%' || Name || '%';
But it seems like a wrong query. It is printing all the rows in the table and I don't know why.
Can someone please help me in this? Are there any other ways to write this query? Is my query wrong?
This should do it for you...
SELECT
a.FullName
FROM
Employee a
WHERE
a.FullName LIKE CONCAT('%', a.Name, '%')
Related
I have a teachers table that looks like this:
teacherid
teacherfname
teacherlname
salary
1
Alexander
Bennett
55.30
I would like to return any record that contains a given string in the teacherfname, teacherlname and salary columns.
What I have right now (this returns exact match only):
SELECT * FROM `teachers` WHERE 'Alexander' IN (teacherfname, teacherlname, salary)
What I would like to do is something like this (this would not return anything):
SELECT * FROM `teachers` WHERE '%Alex%' IN (teacherfname, teacherlname, salary)
What do I need to make the query work? Thank you.
I would assume that the value %Alex% won't ever match the salary column. If you want to search for any rows where the first name or last name include "Alex" I would use simple pattern matching, and force all comparisons to use the same letter case.
For example:
SELECT *
FROM `teachers`
WHERE lower(teacherfname) like '%alex%'
or lower(teacherlname) like '%alex%'
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 did some searching and from one question already posted on stackexchange, the answer was that it was not possible, but I figured to ask. I did not know if it was possible to form a SELECT query to dynamically select which columns will be displayed in a mysql SELECT statement result. Example:
Say I have column names Person, ID, Phone Number, Alt Number for this table:
John | 79 | 800-499-0000 | 800-499-5555
I would like to form a SELECT statement so that it will only pull down columns where string '800-499' is somewhere in the field. Thus the result from MySQL ideally would be:
800-499-0000 | 800-499-5555
The only problem is that I do not think dynamically selecting columns is possible.
Any help or confirmation is appreciated.
You could try something like:
select * from
(select concat(case when col1 like '%800-499-0000%' then concat('col1: ',col1,';') end,
case when col2 like '%800-499-0000%' then concat('col2: ',col1,';') end,
...
case when coln like '%800-499-0000%' then concat('coln: ',coln,';') end)
as search_results
from my_table) sq
where search_results is not null
I have 2 columns: first_name, last_name. That's it.
When a user searches for a name, I want both columns to act as one. (as a full name).
So when someone searches for "Alex Johnson", I want it to query both columns as if they were one column.
Will something like this help:
ALTER TABLE your_table_name ADD FULLTEXT(first_name, last_name);
Try to use concat in your query.
like:
select conact(first_name, last_name) as fullname from ...
detailed description you will find here:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat
You can concat the columns together to search on them such that a simply query would look like this:
create temporary table test(n1 varchar(10), n2 varchar(10));
insert into test values('tommy','bobby');
select * from test where n1+n2='tommybobby';
+--------+--------+
| n1 | n2 |
+--------+--------+
| tommy | bobby |
+--------+--------+
Full more advanced Full Text searching it sounds like you want to use the Match function.
SELECT n1, n2 FROM test
WHERE MATCH (n1,n2)
AGAINST ('+tommy +bobby' IN BOOLEAN MODE);
See MySQL FULLTEXT Indexing and Searching for more info.
*edited because I forgot about the minimum word size.
I am trying to find records that has the following scenario.
ID | name | email
1 Robert robert#gmail.com
2 William bill#gmail.com
3 Michael michael#gmail.com
4 Micahel mike#gmail.com
Based on the above table, I want to find the records where the "name" is contained in the "email field", here record 1 and 3 should be the output and not 2 and 4. Is there any way I can do this comparison?
I tried reading about regex but couldn't find anything. If it's comparison of same value, it will be straightforward, but I am not having any clue for this one. I thought of LIKE but looks like this cannot have field names.
The exact syntax will depend on how you want to define the relationship.
Are you looking for the name anywhere in the email address? (This will be slow)
select id,name,email
from your_table
where email like concat('%',name,'%')
Just at the beginning of the email address?
select id,name,email
from your_table
where email like concat(name,'%')
Just before the # sign?
select id,name,email
from your_table
where email like concat(name,'#%')
You can use LIKE, you just have to use it in combination with CONCAT.
SELECT
ID,
name,
email
FROM
yourTable
WHERE
email LIKE CONCAT(name, '%');
The CONCAT will return a string which can be used to match against email via LIKE.
This should work
SELECT * FROM table WHERE email LIKE (CONCAT('%',name,'%'))
select * from your_table where lower(substring_index(email,'#',1))=lower(name)