I'm having a bit of a problem with join SUM in fuel php.
When I use it like this
$query = DB::select(
'stream_post.*',
'SUM(stream_comment.comment_stream_id)'
)->from('stream_post');
$query->join('stream_comment', 'LEFT');
$query->on('stream_post.stream_id', '=', 'stream_comment.comment_stream_id');
$query->join('users_metadata');
$query->on('stream_post.user_id', '=', 'users_metadata.user_id');
$query->limit(10);
$query->order_by('stream_id', 'DESC');
$result = $query->execute();
if(count($result) > 0) {
foreach($result as $row)
{
$data[] = $row;
}
return $data;
}
I get this error
Column not found: 1054 Unknown column 'SUM(stream_comment.comment_stream_id)' in 'field
What do im doing wrong?
You need to use the expr function to create an expression in the select statement
$result = DB::select(DB::expr(' SUM(stream_comment.comment_stream_id) as count'))->from('stream_post')->execute();
Documented here http://docs.fuelphp.com/classes/database/usage.html
Related
I want codeigniter search to be match with any one word to column.
Example: Let's Say Search Query is "Fashion Outlet for Mens", Now in table column title is "Fashion Outlet" only so i want search input to be split word by word and match column.
Any Help Please
public function search($query)
{
$q = $this->db->from('tablename')
->like('title',$query)
->get();
return $q->result();
}
Updated Question
public function search_query($query,$limit,$offset)
{
$keywords = explode(' ', $query);
foreach ($keywords as $keyword)
{
$keyword = trim($keyword);
$this->db->or_where("`title` LIKE '%$keyword%'");
$this->db->join('table2', 'tablename.id = table2.id');
$this->db->limit( $limit , $offset );
$this->db->order_by('updated_on','desc');
}
$this->db->get('tablename');
return $this->db->result();
}
Error Got
Not unique table/alias: 'table2'
SELECT * FROM `tablename` JOIN `table2` ON `tablename`.`id` = `table2`.`id` JOIN `table2` ON `tablename`.`id` = `table2`.`id` WHERE `title` LIKE '%fashion%' OR `title` LIKE '%outlet%' ORDER BY `updated_on` DESC, `updated_on` DESC LIMIT 50
Please find the Updated Answer for Order By Keyword Priority.
$keywords = explode(' ', $query);
$this->db->select('*');
$this->db->from('tablename');
$this->db->join('table2', 'tablename.id = table2.id','left');
$orderbyQry = "CASE ";
foreach ($keywords as $key => $keyword)
{
$orderbyQry.="WHEN tablename.title LIKE '%$keyword%' THEN $key ";
$keyword = trim($keyword);
$this->db->or_where("tablename.title LIKE '%$keyword%'");
}
$orderbyQry.=" ELSE 100 END ";
$this->db->limit( $limit , $offset );
$this->db->order_by($orderbyQry,'asc');
$this->db->get();
return $this->db->result();
Please join the table only once, and put the limit and order_by clause out of the loop.
public function search_query($query, $limit, $offset) {
$keywords = explode(' ', $query);
$this->db->select('*');
$this->db->join('table2', 'tablename.id = table2.id');
foreach ($keywords as $keyword)
{
$keyword = trim($keyword);
$this->db->or_where("`title` LIKE '%$keyword%'");
}
$this->db->limit( $limit , $offset );
$this->db->order_by('updated_on','desc');
$this->db->get('tablename');
return $this->db->result();
}
Hello please refer my following code which is in model
public function common_report($where) {
$this->db->select('*');
$this->db->from('collect_data_form_history_db as ch');
$this->db->join('board_artwork_history_db as bh','bh.shop_id = ch.shop_id');
$this->db->where('bh.shop_id', $where);
// $this->db->where('bh.shop_id',$where);
$query = $this->db->get();
return $query->result_array();
}
what i want is all data from both table collect_data_form_history_db and board_artwork_history_db.
I get the data but in the wrong format.
In table 'collect_data_form_history_db ' i have 11 entries and in board_artwork_history_db i have 18 entries.
so data I get should be 29 rows but I get 198 rows means 11 * 18.
please tell me the solution
You need to use left join for that. Try this way
$this->db->join('board_artwork_history_db as bh','bh.shop_id = ch.shop_id', 'left');
$this->db->select('t1.*, t2.*')
->from('collect_data_form_history_db t1, board_artwork_history_db t2')
->where('t1.shop_id = t2.shop_id')
->where('t2.shop_id', $where);
$query = $this->db->get();
Try full outer join
public function common_report($where) {
$this->db->select('*');
$this->db->from('collect_data_form_history_db as ch');
$this->db->join('board_artwork_history_db as bh','bh.shop_id = ch.shop_id', 'full outer');
$this->db->where('bh.shop_id', $where);
// $this->db->where('bh.shop_id',$where);
$query = $this->db->get();
return $query->result_array();
}
I've been able to get the query result I need using the following raw sql:
select `person`.`id`, `full_name`, count(actions.user_id) as total
from `persons`
left join `actions`
on `actions`.`person_id` = `persons`.`id`
and `actions`.`user_id` = $user
where `type` = 'mp'
group by `persons`.`id`
But I haven't been able to get it working in eloquent yet.
Based on some similar answers, I'd tried functions within ->where() or leftJoin(), but the count of each person's actions isn't yet being filtered by $user. As it stands:
$query = Person::leftJoin('actions', function($q) use ($user)
{
$q->on('actions.person_id', 'persons.id')
->where('actions.user_id', $user);
})
->groupBy('persons.id')
->where('type', 'foo')
//->where('actions.user_id', '=', $user)
->get(['persons.id', 'full_name', DB::raw('count(actions.id) as total')]);
I'm at least heading in roughly the right direction, right...?
If it's relevant, the Persons.php model has two actions relationships:
public function actions()
{
return $this->hasMany('Action');
}
public function actionsUser($id)
{
return $this->hasMany('Action')->where('user_id', $id);
}
So, for reference, I solved it like so:
$query = Person::leftJoin('actions', function($q) use ($user)
{
$q->on('actions.person_id', '=', 'persons.id')
->where('actions.user_id', '=', "$user");
})
->groupBy('persons.id')
->where('type', 'foo')
->get(['persons.id', 'full_name', DB::raw('count(actions.id) as total')]);
The ->where() clause within leftJoin, oddly, needs the speech marks for the variable to be passed through the sql query correctly (likewise, '2' doesn't seem to work while "2" does).
I found that the where doesn't always work on the leftJoin clause
If in the future you get any trouble with it, I'd suggest you using this:
$query = Person::leftJoin('actions', function($q) use ($user)
{
$q->on('actions.person_id', '=', 'persons.id')
->on('actions.user_id', '=', "$user");
})
->groupBy('persons.id')
->where('type', 'foo')
->get(['persons.id', 'full_name', DB::raw('count(actions.id) as total')]);
Hope it helps someone.
When laravel eloquent just start getting complex like this
For more flexibility and readability I'll just use plain sql statement then hydrate the results.
$sql = "
SELECT `person`.`id`,
`full_name`,
count(actions.user_id) AS total
FROM `persons`
LEFT JOIN `actions`
ON `actions`.`person_id` = `persons`.`id`
AND `actions`.`user_id` = $user
WHERE `type` = 'mp'
GROUP by `persons`.`id`
";
$query = Person::hydrate(
DB::select( $sql )
);
I want to run following query in symfony doctrine.
SELECT p.id AS id FROM skiChaletPrice p WHERE ski_chalet_id = ? AND month = ?
I wrote my doctrine query as following.
$q = Doctrine_Query::create()
->select('p.id AS id')
->from('skiChaletPrice p')
->andWhere('ski_chalet_id = ?', $chaletId)
->andWhere('month = ?', $from);
$result = $q->fetchOne();
if ($result->count() > 0) {
return $result->toArray();
} else {
return null;
}
But my result always include all columns in the table. What the issue? Please help me.
The issue is that fetchOne() will return a Doctrine object, which implicitly contains all the columns in the table. $result->toArray() is converting that doctrine object to an array, which is why you get all the columns.
If you only want a subset of column, don't hydrate an object, instead do something like this:
$q = Doctrine_Query::create()
->select('p.id AS id')
->from('skiChaletPrice p')
->andWhere('ski_chalet_id = ?', $chaletId)
->andWhere('month = ?', $from);
$results = $q->execute(array(), Doctrine::HYDRATE_SCALAR);
See http://docs.doctrine-project.org/projects/doctrine1/en/latest/en/manual/data-hydrators.html
This is how I should do it:
$result = Doctrine_Query::create()
->select('id')
->from('skiChaletPrice')
->andWhere('ski_chalet_id = ?', $chaletId)
->andWhere('month = ?', $from)
->limit(1)
->fetchOne(array(), Doctrine_Core::HYDRATE_SINGLE_SCALAR);
// result will be a single id or 0
return $result ?: 0;
// if you want array($id) or array() inseatd
// return (array) $result;
My simple query:
$list = Doctrine_Query::create()
->from('liste l')
->where('l.id =?', $id)
->fetchOne();
$id = 123;
I know that there is no entry with the $id =123 in my database. When I count $list, I get the result 1. How do I know with my query or the result of my query that there is no entry with the $id = 123 in my database?
var_dump gives me false!
So I just do a quick:
if ($list == FALSE) {
....}