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%'
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%'
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%'
I m trying to query a database with about 2000 entries. I want to select the entries in which the names may contain any one of the vowel.
I tried using the following query, but it gives me those entries that contain all the given characters in that order.
select * from myTable where name like '%a%e%i%';
How do I modify the above query to select those entries with names that may contain at least anyone of the vowels.
Try this for SQL Server:
SELECT * FROM myTable WHERE name LIKE '%[AEIOU]%';
I hope this helps you...
SELECT * FROM myTable WHERE name REGEXP 'a|e';
or.....
SELECT * FROM myTable WHERE name REGEXP 'a|e|i';
In SQL Server, you would do:
where name like '%[aeiou]%';
In MySQL, you would do something similar with a regular expression.
Use OR like this.
This will work for both SQL Server and MySql.
select * from myTable where name like '%a%' OR name like '%e%' OR name like '%i%';
Use LIKE and OR.
Query
select * from myTable
where name like '%a%'
or name like '%e%'
or name like '%i%'
or name like '%o%'
or name like '%u%'
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';
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)