I have a table in which there are three fields
1. city
2. name
3. country
I am available with auto suggest in single Search field as comma seperated values for above given three fields, Now when I write anything on Text field of search and click search i must get all relevant resault. But my query seems to wrong as
I have writtent the WHERE clause as
SELECT * FROM mytable WHERE city LIKE '%$xyz%' OR name LIKE '%$xyz%' OR country LIKE '%$xyz%'
NOTE: while giving input in the search field I DO NOT SELECT ANY AUTOSUGGESTED VALUE
Please Help me to Rectify my query
You need single quotes in your LIKE clauses:
SELECT * FROM mytable WHERE city LIKE '%$xyz%' OR name LIKE '%$xyz%' OR country LIKE '%$xyz%'
SELECT *
FROM mytable
WHERE name like '%$name%' OR country like '%$country%' OR city like '%$city%'
Related
I'm trying to make a search feature that will search multiple columns to find a keyword based match. This query:
SELECT title FROM pages LIKE %$query%;
works only for searching one column, I noticed separating column names with commas results in an error. So is it possible to search multiple columns in mysql?
If it is just for searching then you may be able to use CONCATENATE_WS.
This would allow wild card searching.
There may be performance issues depending on the size of the table.
SELECT *
FROM pages
WHERE CONCAT_WS('', column1, column2, column3) LIKE '%keyword%'
You can use the AND or OR operators, depending on what you want the search to return.
SELECT title FROM pages WHERE my_col LIKE %$param1% AND another_col LIKE %$param2%;
Both clauses have to match for a record to be returned. Alternatively:
SELECT title FROM pages WHERE my_col LIKE %$param1% OR another_col LIKE %$param2%;
If either clause matches then the record will be returned.
For more about what you can do with MySQL SELECT queries, try the documentation.
If your table is MyISAM:
SELECT *
FROM pages
WHERE MATCH(title, content) AGAINST ('keyword' IN BOOLEAN MODE)
This will be much faster if you create a FULLTEXT index on your columns:
CREATE FULLTEXT INDEX fx_pages_title_content ON pages (title, content)
, but will work even without the index.
1)
select *
from employee em
where CONCAT(em.firstname, ' ', em.lastname) like '%parth pa%';
2)
select *
from employee em
where CONCAT_ws('-', em.firstname, em.lastname) like '%parth-pa%';
First is usefull when we have data like : 'firstname lastname'.
e.g
parth patel
parth p
patel parth
Second is usefull when we have data like : 'firstname-lastname'. In it you can also use special characters.
e.g
parth-patel
parth_p
patel#parth
Here is a query which you can use to search for anything in from your database as a search result ,
SELECT * FROM tbl_customer
WHERE CustomerName LIKE '%".$search."%'
OR Address LIKE '%".$search."%'
OR City LIKE '%".$search."%'
OR PostalCode LIKE '%".$search."%'
OR Country LIKE '%".$search."%'
Using this code will help you search in for multiple columns easily
SELECT * FROM persons WHERE (`LastName` LIKE 'r%') OR (`FirstName` LIKE 'a%');
Please try with above query.
How to select data from two columns in same table in a single query.
for example select name "waseem" from username if not found check in city row.
select * from <table_name> where name like 'waseem' or city like 'waseem';
I agree with other's answers but with slight modification. If there is a record waseem ahemed and your only looking for waseem wont be searched in other answered query.
You need to use the wildcard % if you want to search for waseem.
I highly suggest you not to use * for selecting the column names because of the performance issues.
SELECT name, username, password WHERE name LIKE '%waseem%' OR city LIKE '%waseem%'
This is working for me
SELECT * FROM table WHERE row_name1 LIKE 'value' OR row_name2 LIKE 'value'
Hy, i have a sql tabl name kill, with fields like
ID
name
lname.
There are several names are same, like ali, kiran etc, i want to show all the people with the name ali, so i tried this
SELECT * FROM ask WHERE name LIKE 'ali'
but it shows only the last added ali, please will you tell me the right way to do this. thanks
IF you are trying to find all values containing ali for e.g.
Bali
Alison
etc...
What you need to do is run a wildcard search query, so try this:
SELECT * FROM ask WHERE name LIKE '%ali%'
This will find all values where name contains part of ali in it.
If you want to find all names ending in ali, you can do this:
SELECT * FROM ask WHERE name LIKE '%ali'
If you want to find all names starting with ali, you can do this:
SELECT * FROM ask WHERE name LIKE 'ali%'
etc...
I prefer to use REGEXP, for example:
SELECT * FROM ask WHERE name REGEXP 'ali';
Your query should be as below:-
SELECT * FROM ask WHERE name LIKE '%ali%'
Check this link for detailed info.
SELECT * FROM ask WHERE name LIKE '%ali%';
The Syntax for query in your case is
SQL LIKE Syntax
SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern;
You should use query below to selects all names starting with the letter "ali":
SELECT * FROM ask WHERE name LIKE 'ali%';
You should use query below to selects all names ending with the letter "ali":
SELECT * FROM ask WHERE name LIKE '%ali';
You should use query below to selects all names containing the letter "ali":
SELECT * FROM ask WHERE name LIKE '%ali%';
Refer This link for tutorials on Like.
OK, I have a DB table that's called players and each player has a forename and surname. Then I have a PHP Ajax search thing that I call to search for players. For example... in the input box, someone types James and there's a row in the table with forename and surname James and Smith respetively.
I do this $check = mysql_query("SELECT * FROMplayersWHEREsurnameLIKE '%$name%' ORforenameLIKE '%$name%' LIMIT 0, 10") or die(mysql_error());
And it returns at least 10 with either forename or surname like the keyword James. However, if I type James Smith, despite it being in the table, I get zero results.
How do I fix this?
Are you using InnoDB or MyISAM? If your using MyISAM, you can create a single field which holds the combined name and then search it using a full text index. So lets imagine you add a new field called combined_names you would search it like this
SELECT * FROM table WHERE match(combined_names) against('John Smith');
This would find any row with either John or Smith in, you can change it to match only those rows with both parts you would add plusses like so:
SELECT * FROM table WHERE match(combined_names) against('+John +Smith');
Here is the documentation on the MySQL site where you can find out more:
http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
SELECT * FROM players WHERE CONCAT(forename, ' ', surname) = '$name' OR forename LIKE '%$name%' OR surname LIKE '%$name%'
split the name up on spaces so the query runs twice (if there is one space)
the query will run for both names
$nameBits = explode($name," ");
run the query for each piece of $nameBits
surname LIKE '%$nameBits[$i]%'
here's my table:
and I want get the customers which have some values of for/category fields which is comma separated..
I am trying something like this:
SELECT * FROM `customers` WHERE `for` LIKE ('%AMC PHD & WWS%' OR '%Rostfrei%' OR '%Thermopac%')
but its giving empty result.
RedFilter's SQL is correct, but you should also know that "for" is a MySQL reserved word. You should avoid using it as a column name, or wrap it in backticks when you use it:
SELECT *
FROM customers
WHERE `for` LIKE '%AMC PHD & WWS%'
OR `for` LIKE '%Rostfrei%'
OR `for` LIKE '%Thermopac%';
The alternative, typing the column name once is:
SELECT * FROM customers WHERE `for` REGEXP 'AMC PHD \& WWS|Rostfrei|Thermopac';
Try:
SELECT *
FROM customers
WHERE for LIKE '%AMC PHD & WWS%'
or for LIKE '%Rostfrei%'
or for LIKE '%Thermopac%'