Sql query with multiple clauses - mysql

I have a search form something like
My Database table looks like
If user type compay name and city i can search that using
if (!empty($_POST["company"]) && !empty($_POST["city"])) {
$company = mysqli_real_escape_string($conn,$_POST["company"]) ;
$city = mysqli_real_escape_string($conn,$_POST["city"]) ;
$result = mysqli_query($conn, "SELECT * FROM companies_active_accounts WHERE Company_Name='$company' AND City='$city'");
}
But I want such a query that first box should contain company but second box can contain city or state. How to write query for that so that it searches for company with city/state.
Since city/state are same box. I have given the input field name of "city".
Thank You very much!

you could use a or condition
SELECT *
FROM companies_active_accounts
WHERE Company_Name='$company'
AND ( City='$city' OR State = '$city')

Related

How do I search a term with space to find exact/similar results [duplicate]

I am new to php and mysql. In my project I have to develop search engine for multiple keywords on multiple columns and also require the keyword ranking and relevency.
For example if I have three words hi, hello, hey and want to search on fields like subject, message, reference, textbody. in this case the row who has more keywords ranking will come first and lowest ranker row goes last. will any one guide me how to implement such search engine. my database is about 4 million and is growing rapidly.
Use Full text search :
<?PHP
//SET THE SEARCH TERM
$term = "Search Term";
$sql = "SELECT *, MATCH(subject, message,reference,textbody) AGAINST('". $term ."') as score FROM pages WHERE MATCH (subject, message,reference,textbody) AGAINST('". $term ."') ORDER BY score DESC";
$query = mysql_query($sql);
//BUILD A LIST OF THE RESULTS
while($result = mysql_fetch_assoc($query)) {
// your stuff for showing result
}
?>
Note: you should have FULLTEXT indexing on the columns subject, message,reference,textbody
You can use the AND or OR operators with like keyword, depending on what you want the search to return.
SELECT * FROM table WHERE subject LIKE %$param1% AND another_col LIKE %$param2% .......;
OR
SELECT * FROM table WHERE subject LIKE %$param1% OR another_col LIKE %$param2% .......;

I want to write a multiple condition query to display result from Table

I have a table named info which has 3 columns: Id, city, country.
I need to filter the table. That is, when i search for city and country it should show me the exact row but when i will search only for the country it should show all the cities in that country. how can i write the condition?
How it is looking right now
I have tried using AND and OR but they only do one task. my current query
SELECT * FROM info WHERE name='$_POST[name1]' AND country='$_POST[country1]';
this work for both fields and
SELECT * FROM info where name='$_POST[name1]' OR country='$_POST[country1]';
this fails when searched on both fields at once
Try this :
$condition = '';
$query = ''
if(!empty($_POST[name1])) { $condition .= "and name LIKE '$_POST[name1]' "; }
if(!empty($_POST[country1])){ $condition .= "and country LIKE '$_POST[country1]' "; }
$query = "SELECT * FROM info WHERE Id !='' " . $condition;
mysqli_query($connection,$query);
Both name and country are varchar so use LIKE in this way:
SELECT * FROM info WHERE name LIKE '$_POST[name1]' OR country LIKE '$_POST[country1]';
try this.

Search query to fetch data

I want write search query which fetch the data from students table based on multiple conditions like by ID , by name and by date of birth like that.
if I use OR condition like
Select * from students where Id='101' or name='Kumar' or age='21';
it will return if any one field is entered. If multiple fields are entered it will consider first only.
If I use AND condition like
Select * from students where Id='101' and name='Kumar' and age='21';
It will return only if all fields are entered. If any one of the field is empty it will return zero, I mean it will return empty. I want to get result even some fields are empty.
I'm using this quarry for swing application if I haven't enter text in one of the field then it will become empty like I'd='' not null
How to get Result at this condition
Select * from students where Id='' and name='' and age='21'
I want to get result even some fields are empty.
Then include that in your conditions. Something like this:
WHERE
(Id = '101' OR Id IS NULL) AND
(name = 'Kumar' OR name IS NULL) AND
(age = '21' OR age IS NULL)
Nesting conditions in parentheses allows you to build fairly complex conditional logic. How you define that logic for finding the records you want to find is really up to you.
Use LIKE clause without %s. So it'll be acting as a simple =. Here's how it will work.
if(txtId.getText().equals("")){
String id = "%%";
}else{
String id = txtId.getText();
}
if(txtName.getText().equals("")){
String name = "%%";
}else{
String name = txtName.getText();
}
if(txtAge.getText().equals("")){
String age = "%%";
}else{
String age = txtAge.getText();
}
resultset = statement.executeQuery("SELECT * FROM student WHERE id LIKE '"+ id +"' and name LIKE '"+ name +"' and age LIKE '"+ age +"';");
Try this out. It'll work like a charm. (It's up to you to change the query according to your database)

Mysql like search with more than one column

I have a drupal site with the search option. If user enters the search keyword, i need to compare that with more than one columns and display the records.
I have tried the following query
$search = 'test';
$sql_query = db_select('logoinfo', 'l')->fields('l');
$or = db_or();
$or->condition('search_field', '%'.db_like($search).'%','LIKE');
$or->condition('companyname', '%'.db_like($search).'%','LIKE');
$sql_query->condition($or);
$selectlogos = $sql_query->execute();
It displays all the records matching the search keyword with the order of auto increment Id asc.
But i want to display the records first which is having both search_field and companyname matches with the keyword, after that other records which is matches with either companyname or search_field. Please advise to achieve this.
Since orderBy requires a field name and can't order by an expression, you'll need to use addExpression to get an alias and then order by that alias. The expression in my example will return 0 if the value is not in both fields and 1 if it is in both fields. As far as I know this should be standard SQL, but it may vary on different database backends; so the expression may need to be adjusted depending on the database you are using.
<?php
$search = 'test';
$sql_query = db_select('logoinfo', 'l')->fields('l');
$or = db_or();
$or->condition('search_field', '%'.db_like($search).'%','LIKE');
$or->condition('companyname', '%'.db_like($search).'%','LIKE');
$sql_query->condition($or);
$safe_search = db_like($search);
$ex_alias = $sql_query->addExpression("l.search_field LIKE '%$safe_search%' AND l.companyname LIKE '%$safe_search%'");
$sql_query->orderBy($ex_alias, 'DESC');
$selectlogos = $sql_query->execute();
?>

php search engine key words ranking

I am new to php and mysql. In my project I have to develop search engine for multiple keywords on multiple columns and also require the keyword ranking and relevency.
For example if I have three words hi, hello, hey and want to search on fields like subject, message, reference, textbody. in this case the row who has more keywords ranking will come first and lowest ranker row goes last. will any one guide me how to implement such search engine. my database is about 4 million and is growing rapidly.
Use Full text search :
<?PHP
//SET THE SEARCH TERM
$term = "Search Term";
$sql = "SELECT *, MATCH(subject, message,reference,textbody) AGAINST('". $term ."') as score FROM pages WHERE MATCH (subject, message,reference,textbody) AGAINST('". $term ."') ORDER BY score DESC";
$query = mysql_query($sql);
//BUILD A LIST OF THE RESULTS
while($result = mysql_fetch_assoc($query)) {
// your stuff for showing result
}
?>
Note: you should have FULLTEXT indexing on the columns subject, message,reference,textbody
You can use the AND or OR operators with like keyword, depending on what you want the search to return.
SELECT * FROM table WHERE subject LIKE %$param1% AND another_col LIKE %$param2% .......;
OR
SELECT * FROM table WHERE subject LIKE %$param1% OR another_col LIKE %$param2% .......;