convert the mysql query to codeigniter 3 model - mysql

Convert the MySQL query to Codeigniter Query
$query = "(SELECT content, title, 'msg' as type FROM messages WHERE content LIKE '%" .
$keyword . "%' OR title LIKE '%" . $keyword ."%')
UNION
(SELECT content, title, 'topic' as type FROM topics WHERE content LIKE '%" .
$keyword . "%' OR title LIKE '%" . $keyword ."%')
UNION
(SELECT content, title, 'comment' as type FROM comments WHERE content LIKE '%" .
$keyword . "%' OR title LIKE '%" . $keyword ."%')";
mysql_query($query);
I have tried to convert it in Codeigniter
$this->db->like("content", $keyword);
$this->db->or_like('title',$keyword,'after');
$this->db->or_like('msg',$keyword,'after');
->from('message')
$this->db->like("content", $keyword);
$this->db->or_like('title',$keyword,'after');
$this->db->like("msg", $keyword);
->from('topics')
$this->db->or_like('content',$keyword,'after');
$this->db->or_like('title',$keyword,'after');
$this->db->or_like('msg',$keyword,'after');
->from('comment')
The top one is in MySQL and bottom which I try to convert is in Codeigniter I m trying to search the keyword from selected columns from three tables. How I can convert the MySQL to Codeigniter. I'm trying to search the keyword from selected columns from three tables.
How I can convert the MySQL to Codeigniter

Try this
$this->db->select('content, title, msg as type');
$this->db->from('message');
$this->db->like("content", $keyword);
$this->db->or_like('title',$keyword,'after');
$this->db->or_like('msg',$keyword,'after');
$query1 = $this->db->get_compiled_select();
$this->db->select('content, title, msg as type');
$this->db->from('topics');
$this->db->like("content", $keyword);
$this->db->or_like('title',$keyword,'after');
$this->db->like("msg", $keyword);
$query2 = $this->db->get_compiled_select();
$this->db->select('content, title, msg as type');
$this->db->from('comment');
$this->db->or_like('content',$keyword,'after');
$this->db->or_like('title',$keyword,'after');
$this->db->or_like('msg',$keyword,'after');
$query3 = $this->db->get_compiled_select();
$result = $this->db->query($query1." UNION ".$query2." UNION ".$query3);
return $result->result();
Note:- If you intend to use this make sure that your both the table column are same sequence and name.

Related

Laravel eloquent: Having and where in the same query but I want ti to be OR [duplicate]

Currently I am working on a project in Laravel but I am stuck.I want to create a SQL statement like this:
SELECT * FROM SPITems WHERE publisher_id=? AND feed_id=? AND (title LIKE '%?%' OR description LIKE '%?%')
Now I have this code:
$query = SPItem::orderBy('title');
if(isset($_GET['publisherID']) && is_numeric($_GET['publisherID']))
{
$query = $query->where('publisher_id', $_GET['publisherID']);
}
if(isset($_GET['productFeedID']) && is_numeric($_GET['productFeedID']))
{
$query = $query->where('program_id', $_GET['feedID']);
}
if(isset($_GET['search']))
{
$query = $query->orWhere('title', 'like', '%' . $_GET['search'] . '%');
$query = $query->where('description', 'like', '%' . $_GET['search'] . '%');
}
But that generates:
SELECT * FROM SPITems WHERE (publisher_id=? AND feed_id=?) OR (title LIKE '%?%') AND description LIKE '%?%'
How can I get the correct "or" order?
Check out the Logical Grouping section in the docs:
https://laravel.com/docs/master/queries#logical-grouping
It explains how to group conditions in the WHERE clause.
It should be something like:
if(isset($_GET['search']))
{
$query->where(function($query){
$query->where('title', 'like', '%' . $_GET['search'] . '%')
->orWhere('description', 'like', '%' . $_GET['search'] . '%');
});
}
You can use whereRaw
SPItem::whereRaw(" publisher_id=? AND feed_id=? AND (title LIKE '%?%' OR description LIKE '%?%')", array(?,?,?,?))

How to build a search app with node js and mysql [duplicate]

I have three tables in my database which are:
messages
topics
comments
Each of these tables has two fields called 'content' and 'title'. I want to be able to use 'Like' in my sql statement to look at 'messages.content', 'messages.title', 'topics.content', 'topics.title', 'comments.content' and 'comments.title' using a keyword.
So far, my query is able to find results from only one table:
mysql_query("SELECT * FROM messages
WHERE content LIKE '%" . $keyword . "%'
OR title LIKE '%" . $keyword ."%'");
I am also wondering, once I get the results from multiple tables, how can I tell what result is from what table?
Any help would be greatly appreciated!
$query = "(SELECT content, title, 'msg' as type FROM messages WHERE content LIKE '%" .
$keyword . "%' OR title LIKE '%" . $keyword ."%')
UNION
(SELECT content, title, 'topic' as type FROM topics WHERE content LIKE '%" .
$keyword . "%' OR title LIKE '%" . $keyword ."%')
UNION
(SELECT content, title, 'comment' as type FROM comments WHERE content LIKE '%" .
$keyword . "%' OR title LIKE '%" . $keyword ."%')";
mysql_query($query);
So, you are getting result from all of the three tables, and you can identify which row came from which table by looking at its type value.
What you are probably looking for is the UNION command:
SELECT id, 'messages' as 'table' FROM messages
WHERE content LIKE '%keyword%'
OR title LIKE '%keyword%'
UNION
SELECT id, 'topics' as 'table' FROM topics
WHERE content LIKE '%keyword%'
OR title LIKE '%keyword%'
UNION
SELECT id, 'comments' as 'table' FROM comments
WHERE content LIKE '%keyword%'
OR title LIKE '%keyword%'
Two search in other tables you use:
SELECT `categories`.`title`, `posts`.`title` WHERE `categories`.`title` LIKE {$a} OR `posts`.`title` LIKE {$a}
The CATEGORIES and POSTS are tables of your database.
Html Search form:
<div class="header_top_right">
<form action="search.php" method="GET" class="search_form">
<input type="text" placeholder="Text to Search.." name="search">
<input type="submit" class="btn btn-default" value="">
</form>
</div>
Search.php:
<?php
if (isset($_GET['search']) || !empty($_GET['search'])) {
$search = mysqli_real_escape_string($db->link, $fm->validation($_GET['search']));
}
else{
header("Location:404.php");
}
?>
<?php
$query = "SELECT * FROM news_post WHERE title LIKE '%$search%' OR body LIKE '%$search%' OR tags LIKE '%search%'";
$post = $db->select($query);
if ($post) {
while ($result = $post->fetch_assoc()) {
echo"Database data like, $result['title']";
}
}
else{
echo "result Not found";
}
include database.php in search.php
class Database{
public function select($query){
$result = $this->link->query($query) or die($this->link->error.__LINE__);
if($result->num_rows > 0){
return $result;
}
else {
return false;
}
}
}
$db = new Database();

MySQL search multiple tables and in one table search multiple columns

I built a query for searching one value across multiple tables. It works great as is, but I want to search multiple fields in the "customers" table ("last_name" and "company_name" additionally).
$sql = "SELECT first_name as name FROM customers WHERE first_name LIKE '%" . $keyword . "%'
UNION
SELECT name as name FROM events WHERE name LIKE '%" . $keyword . "%'
UNION
SELECT product_name as name FROM products WHERE product_name LIKE '%" . $keyword . "%'";
Do I just add more separate lines for each additional field like so?
"SELECT first_name as name FROM customers WHERE first_name LIKE '%" . $keyword . "%'
UNION
SELECT last_name as name FROM customers WHERE last_name LIKE '%" . $keyword . "%'
UNION
SELECT company_name as name FROM customers WHERE company_name LIKE '%" . $keyword . "%'
It doesn't seem the most efficient, so wanted to check. Thanks!
There is an efficient solution. Since you collect only name, you collect them into three different variable. and using backend language, you merge these 3 array.
example for php,
$sql1 = "SELECT first_name as name FROM customers WHERE first_name LIKE '%" . $keyword . "%';
//getting first array result by this query
$sql2 = "SELECT name as name FROM events WHERE name LIKE '%" . $keyword . "%'";
//getting second array result by this query
$sql3 = "SELECT product_name as name FROM products WHERE product_name LIKE '%" . $keyword . "%'";
//getting third array result by this query
$result = array_merge($sql1, $sql2, $sql3)
This solution will be applicable, if you can manage data by your backend language.

Right way to write multiple options search query

Belove is My search Query which works fine if I want any one of the passed value search , but what if i want exact match for particular value ?
I can use AND operator but that will increase number of lines ,for example how can i write query if I want particular location match ,pls can any one help me out with better search query
$sql = "SELECT * ";
$sql .= "FROM js_projects WHERE ";
$sql .= "jsp_title LIKE '%" . $valToSearch . "%' ";
$sql .= "OR jsp_nature LIKE '%" . $nature . "%' ";
$sql .= "OR jsp_etype LIKE '%" . $etype . "%' ";
$sql .= "OR jsp_location LIKE '%" . $location . "%' ";
$sql .= "OR jsp_date LIKE '%" . $jobdate . "%' ";
Edit
Suppose I have value for
valtosearch,location,jobdate,etype and I want exact match for all this values ,
or I can have any two of these values and I want exact match for them ,
or I can have any one of these values and I want exact match for that .
So how can I write query for all these possiblities?
Try like this. This will work fine
$condition=($location!=''?" AND jsp_location LIKE '%".$location."%":"");
$condition.=($jobdate!=''?" AND jsp_date LIKE '%".$jobDate."%":"");
$sql = "SELECT * FROM js_projects WHERE jsp_title LIKE '%".$valueToSearch."%' ".$condition;

Search the records of visible = 1

I have a problem
when I get records from the database
mysql_select_db($database_Photo_con, $Photo_con);
$query_rsPhoto = "SELECT * FROM photographs WHERE visible = 1 ORDER BY id ASC";
This shows me all records with visible = 1
if search
mysql_select_db($database_Photo_con, $Photo_con);
$query_rsPhoto = sprintf("SELECT * FROM photographs WHERE caption LIKE %s OR caption_2 LIKE %s AND visible = 1 ", GetSQLValueString("%" . $colname_rsPhoto . "%", "text"),GetSQLValueString("%" . $colname_rsPhoto . "%", "text"));
it shows me all
Does anyone know why this is happening?
It is because the operator precedence (AND has higher priority than OR), so you should use brackets.
$query_rsPhoto = sprintf("SELECT * FROM photographs WHERE (caption LIKE %s OR caption_2 LIKE %s) AND visible = 1 ", GetSQLValueString("%" . $colname_rsPhoto . "%", "text"),GetSQLValueString("%" . $colname_rsPhoto . "%", "text"));
And if the GetSQLValueString do not return string with quotes, you have to add them in the query
$query_rsPhoto = sprintf("SELECT * FROM photographs WHERE (caption LIKE '%s' OR caption_2 LIKE '%s') AND visible = 1 ", GetSQLValueString("%" . $colname_rsPhoto . "%", "text"),GetSQLValueString("%" . $colname_rsPhoto . "%", "text"));