php mysql query issue - php

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

Related

How to search the multiple keywords in multiple columns? [duplicate]

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.

SELECT * FROM tbl name WHERE name LIKE 'ask'

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.

How to select rows from a table where a word appears? (MySQL + PHP)

I have to make a search for keywords as part of my Computer Science work.
I have the names and descriptions of several DVD's.
The user has to search a word, and then displayed are all the names of DVD's where the word appeared in either the title or description.
Let's say my columns in my table
were "dvd title" and "description", and the word the person has entered is $keyword.
How would i select rows in mysql where $keyword appears at least once in either columns "dvd title" and "description".
Thanks for reading. Any help appreciated.
You could create a full text index on those columns, but that probably isn't what they want you do do.
You need wildcards, and to you wildcards compare with the keyword LIKE instead of =. A wildcard in mysql is %
SELECT * FROM mutable WHERE dvdtitle like '%keyword%' or description like '%keyword%';
As for using PHP variable and creating the string, you've got to do some of your own homework.
$sql = "SELECT * FROM table_name WHERE dvdtitle LIKE '%".$keyword."%' OR description LIKE '%".$keyword."%'";
Executing the above SQL query would return all the rows in the table that has the specified keyword in either the column dvdtitle or description.
Use this query:
SELECT *
FROM table_name
WHERE dvd_title like '%$keyword%'
OR description like '%$keyword%'
You can use following code sample, it's not a full code but will give you an idea:
$query_str = "select * from dvd_table_name where dvd_title like '%$keyword%' or description like '%$keyword%'";
$qh = mysql_query($query_str);
Then use mysql_fetch_assoc($qh) to retrive data using while loop;

How to create a common search bar in PHP Mysql

I am working on an application in which i want to create a search bar which will be able to search for individual users as well as brands.
For this i have written a query like this :
SELECT * FROM temp_users,brand_pages WHERE firstname LIKE'%$find%' or lastname LIKE '%$find%' or name LIKE '%$find%'
I checked the query but it is not as needed. What is the way it can be done ??
Should i use joins ??
Yes you need join.
Try this:
SELECT *
FROM temp_users AS TU
INNER JOIN brand_pages AS BP
WHERE (TU.firstname LIKE "%$find%" OR TU.lastname LIKE "%$find%" OR BP.name LIKE "%$find%")
Edit: If you are using this query in PHP and trying to use the variable $find inline, use double quotes (not single quotes).

Searching database mysql

I created a stored procedure to search my database by name. I have many text files saved in database named like: a.txt, a1.txt,a2.txt,a3.txt,...
When i run my stoerd procedure only 1 resoult is showing.
SELECT * FROM cars WHERE name LIKE CONCAT ('%', search ,'%');
What is the problem here?
Your problem solution is
SELECT * FROM cars WHERE name LIKE CONCAT ('%'. $search . '%');
or if you are using directly then on sql server then
SELECT * FROM cars WHERE name LIKE CONCAT ('%search%');
If $search (note the dollar sign) is a PHP variable (I assume that because you tagged the question with "php"), you don't have to use the SQL function CONCAT but join the strings in PHP and send it all to MySQL:
$sql = 'SELECT * FROM cars WHERE name LIKE "%'.$search.'%"';
That's because CONCAT will make your % treated as a character, not a search criterion.
try this
SELECT * FROM cars WHERE name LIKE '%$search%' ;
or this
WHERE name LIKE '$search'

Categories