i need some code for search some data inside DB...
i have two fields that is id and name...
if i type "ABC" it can show all data about "ABC"..
if "DEF"=>all DEF data...or i type anything it can show that "anything"...
i'm just know how to show all data inside fields that makes "ABC" and "DEF" show together,the code like this:
"SELECT ID,Name FROM Regist ORDER BY date";
MySQL wildcards: _ AND % (in conjunction with keyword LIKE)
_ denotes a single character
SELECT name FROM colors WHERE name LIKE 'gr_y';
Output: 'grAy', 'grEy'.
% denotes any number of characters
SELECT name FROM colors WHERE name LIKE 'gr%';
Output: 'grAY', 'grEY', 'grEEN'.
You can use both _ and % multiple times
SELECT name FROM color WHERE name LIKE '_ra%';
Output: 'GraY', 'OraNGE'.
In your case, if you want to list the names containing starting with either 'ABC' or 'DEF', your query would be like this:
SELECT id,name FROM Regist
WHERE name LIKE 'ABC%' OR name LIKE 'DEF%'
ORDER BY date
If you want your names to contain the strings 'ABC' OR 'DEF' anywhere inside, your query would be:
SELECT id,name FROM Regist
WHERE name LIKE '%ABC%' OR name LIKE '%DEF%'
ORDER BY date
Finally, if you want your names to contain both 'ABC' AND 'DEF' you could use:
SELECT id,name FROM Regist
WHERE name LIKE '%ABC%DEF%' OR name LIKE '%DEF%ABC%'
ORDER BY date
Probably you want something like this:
SELECT id,name FROM Regist WHERE NAME LIKE '%ABC%' OR NAME LIKE '%DEF%';
Please note that this kind of search is inefficient and if you need something like that you'd better use something like Sphinx
From the little i understand your question
"SELECT ID,Name FROM Regist where name='ABC' ORDER BY date";
is this correct?
"SELECT id,name FROM Regist WHERE id LIKE '%' OR NAME LIKE '%' ORDER BY date";
Related
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 have a select statement like this:
Select * from A where name like 'a%' or name like 'b%' or name like 'j%' or name like ... etc
Is it possible to store a%, b%, j% in a table somewhere so I can more easily manage this list and convert my query to something like:
Select * from A where name like (Select * from StringPatternToMatch)
Try this:
SELECT * FROM A
JOIN StringPatternToMatch patt ON A.name LIKE '%' + patt.pattern + '%';
Replace patt.pattern with the name of the column in your StringPatternToMatch
You can do a regexp search instead.
select *
from A where name regexp '^[abjf]';
It's easier query to maintain than a ton of or'd likes.
demo here
'^[abjf]' means match the start of the string (^), followed by any of the characters in the list ([abjf]). It doesn't care what comes after that.
Just add more letters to the list if you find names starting with them.
I am using a mysql database for storing the data. The table consists of 2 columns s_no and name. I used index on this table on name. Now I want to get only words starting with a particular letter say "a" or "b" etc. How can I do it? Is there any SQL query for retrieving data in this fashion?
select s_no,name from tablename where name like 'a%' or name like 'b%'
select * from table_name
where name like 'a%'
select s_no,name from table where name like 'a%' or name like 'b%'
Select * from tablename where name like 'a%'
You can also pass the value inside parameters
#value varchar(50)
Select * from tablename where name like #value
so whatever characters u insert like a or b just mearge it with % and here you go
like #value=#value+ '%' before query .
We have a search query that allows the users to enter a string and then searches 3 separate fields via LIKE '%string%'. The fields are:
Name
Description
Type
When then return the results and right now just order them arbitrarily. We would prefer the results be ordered first by results found in the Name field, second by items found in the Description field, and then lastly in the Type field. Because we have paging and sorting also associated with this, we really want it returned in one record set. Is this even possible?
Thanks in advance.
In MySQL, the following should work:
SELECT *
FROM atable
WHERE Name LIKE '%string%'
OR Description LIKE '%string%'
OR Type LIKE '%string%'
ORDER BY
CASE
WHEN Name LIKE '%string%' THEN 1
WHEN Description LIKE '%string%' THEN 2
WHEN Type LIKE '%string%' THEN 3
END
;
Please try this :
Select *
from Table
where Name like '%string%'
and description like '%string%'
and type like '%string%'
order by name,description,type
You can try this:
Select *
from Tablename
where Name like '%string%'
and description like '%string%'
and type like '%string%'
order by name,description,type