MySQL match a value with wildcard in multiple columns - mysql

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

Related

Mysql select column with concat

i want some correction here. i want to select all people with name fred in database
Here's my query:
SELECT * FROM tdble WHERE CONCAT(name) LIKE CONCAT('%', REPLACE('fred', '')'%')
What you are asking can be simply achieved by either using the "=" operator of the wildcard operator "like" statement.
If you wish to find all records that have an exact match to the name 'Fred' then you should model your query as so:
Select * From tdble Where Name = 'fred'
However, if you want to get all results where the names have 'fred' included in it somewhere use the wildcard operator.
Select * From tdble Where Name like '%fred%'
Also you can further model your query to know where exactly in which form you want 'fred' to appear. Example if you want 'Fred' to be as the last characters of your name string, for instance you wish to get names which ends with fred then model your query like this:
Select * From tdble Where Name like '%fred'
(you will get results like 'alfred', provided there is an alfred in your table)
However if you wish to get all names that begin with fred, model the query like this:
Select * From tdble Where Name like 'fred%'
(you will get results like 'fredinane', provided there is a fredinane in your table)
Cheers
If you want to fetch record with name 'fred', you can simply do Select * from TableName Where Name = 'fred'.
If you want to fetch records which their names' string contain 'fred', you have to use select * from TableName where Name like '%fred%'

combining AND operator in mysql

i want to combine conditions by AND operator ..
in my query i want to combine 3 coditions where the result appears when all of them are true.
i want to achive this
SELECT * FROM table WHERE 'condition1' AND ('condition2' AND 'condition3');
i tried this combination but it doesnt work.
this is the actual code
SELECT * FROM planmenu WHERE name LIKE '%$search%' AND type LIKE '%$type%' AND dishcontent LIKE '%$dishcontent%'
where
$search, $type and $dishcontent are php variables. however when i place OR insteade of the second AND it works perfectly. but when i use AND it does not !!
There's nothing wrong with your SQL. Check your data.
create table planmenu (
name varchar(20),
type varchar(20),
dishcontent varchar(20)
);
insert into planmenu values ('a','b','c');
Now if you do this:
SELECT *
FROM planmenu
WHERE name LIKE '%z%'
AND type LIKE '%z%'
AND dishcontent LIKE '%c%';
You'll get zero records back because there are no records matching all three conditions.
If you do this:
SELECT *
FROM planmenu
WHERE name LIKE '%z%'
AND type LIKE '%z%'
OR dishcontent LIKE '%c%';
You'll get a record back because the OR makes it so that the first two conditions OR only the third condition has to be true. It is the equivalent of running this:
SELECT *
FROM planmenu
WHERE (name LIKE '%z%' AND type LIKE '%z%')
OR dishcontent LIKE '%c%';
Check your data. You don't have any records matching all three conditions.
Change the condition to the columns you want to query against, with the values you are trying to get.
SELECT
*
FROM table
WHERE
condition1='something' AND
condition2='somethingelse' AND
condition3='somethingdifferent';

Write a Select Statement to Query

I'm trying to query the first letter of a last name in MySQL. I want to skip the first name and query the a certain letter in the last name. Thanks
In SKU_data, which SKU has a buyer whose last name begins with 'M'?
*/
select *
from sku_data
where buyer ;
In your where clause, search on WHERE buyer.LastName LIKE 'M%'. This will return all results that start with M.
You need to use a combination of the substring method and the substring_index method in your sql query.
Your select query should look something like this
SELECT SUBSTRING(last_name,1,1)
I'm assuming that your name field has both first and last name in it, instead of separate fields for first and last name. Use the substring_index method to figure out what last_name should be:
SUBSTRING_INDEX(full_name,' ',-1)
Combining this SQL, we have:
SELECT SUBSTRING(SUBSTRING_INDEX(buyer,' ',-1),1,1) as first_letter
Now you can use the first_letter field in a where clause to get your desired result:
SELECT *, SUBSTRING(SUBSTRING_INDEX(buyer,' ',-1),1,1) as first_letter
from sku_data
where first_letter = 'M' ;
Assuming that you have a field like say "full_name" which has first name and last name in the same column.
Lets say the full_name in table employee has a value "JEFF MOSTI"
You can simply use the following statement to get what you need
select * from employee where full_name regexp ' M';
I am assuming you are looking for the last name to start with 'M' and that there is a space (' ') between the first and last name.

Sql query returns only one row (LIKE)

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.

mysql select query - to match one field with the other field

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)