Search for: 'chemist'
Problem: query which will match a string like 'onechemist' but not 'chemist'.
SELECT id,name FROM `records`
WHERE name LIKE '%". mysql_real_escape_string($q) ."%'
This alternate try won't work:
SELECT id,name FROM `records`
WHERE name LIKE '%". mysql_real_escape_string($q) ."%'
OR name LIKE '". mysql_real_escape_string($q) ."%'
OR name LIKE '%". mysql_real_escape_string($q) ."'
How could I compile the above into one single query that will match any field which has the string or optimize the query into a better expression?
If $q is holding 'chemist', it will match a name that is also 'chemist`. In that case, your first query should work. Try double checking your values.
$sql = "SELECT `id`, `name` FROM `records`
WHERE `name` LIKE '%".mysql_real_escape_string($q)."%'";
PS - Your 2nd query will pull the same results as your 1st.
You can use regular expression for this. for example,
SELECT id,name FROM `records` where name REGEXP '^onechemist'
It will match names starts with onechemist
Related
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 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 .
This seems to be an easy question but I can't for the life of me find an answer. How do I select all rows in a column regardless of value? For instance if I want everything in the column location:
"SELECT * FROM whateverTable WHERE id='$id' AND date='$date' AND location='I don't care, return everything'"
Keep in mind that I need location in there because I'm getting a dynamic URL query to this database, otherwise I would have just left it out.
Edit
I've read some of the answers and shame on me for not making myself clearer. I'm trying to see if a MySQL query where you can select every row in a column is possible without the need for this:
if ($location == '') {
"SELECT * FROM whateverTable WHERE id='$id' AND date='$date'"
} else {
"SELECT * FROM whateverTable WHERE id='$id' AND date='$date' AND location='$location'"
}
Rather than hacking up my code like that (because I have a hell of alot more query clauses in my real code), I'm trying to see if something like this is possible :
"SELECT * FROM whateverTable WHERE id='$id' AND date='$date' AND location='%'"
or this:
"SELECT * FROM whateverTable WHERE id='$id' AND date='$date' AND location='.*'"
But I'm not having any luck. If it's not, I'll just move on. Sorry for not making myself clearer
As you said in your question, you should build the query dynamically and just omit that field. The query would then become:
SELECT * FROM whateverTable
WHERE id = '$id'
AND date = '$date'
If you can't do that for some reason, you could try something like this:
SELECT * FROM whateverTable
WHERE id = '$id'
AND date = '$date'
AND (location='$location' OR '$location' = '')
Setting $location to the empty string will cause all locations to be returned.
And don't forget to correctly escape your strings to avoid SQL injection attacks.
SELECT location FROM whateverTable will return you every single row/value in the location column.
Add your where clause if you want to start filtering the results down.
If you're dynamically building the query just check if location is null or the empty string and then omit that part of the query.
Replace equal symbol : "=" by LIKE
SELECT * FROM whateverTable WHERE id='$id' AND date='$date' AND location LIKE '%'
I think you're looking for a sub query?
SELECT * FROM whateverTable WHERE location IN (SELECT location FROM whateverTable);
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";