I have this query :
$query->select('if(bud.posa=2,1,0) AS proprietaire,if(bud.du=1 and bud.gog=1,1,0) as pa')->from('bud');
if I execute it is giving synthax error; I try to echo the sql that is being generated I found that it is not being generated(quoting problem) well, here is what is generated:
select if(bud.posa=2, `1`, `0)` AS `proprietaire`, if(bud.du=1 and bud.gog=1, `1`, `0)` AS `pa` FROM `bud`
0) is what is causing the error ,I don't see how to solve it,Any solution?
Rewrite the query like this:
$query->select([
'proprietaire' => 'if(bud.posa=2,1,0)',
'pa' => 'if(bud.posa=2,1,0)',
]);
http://www.yiiframework.com/doc-2.0/yii-db-query.html#select()-detail
Try so:
use yii\db\Expression;
.......
.......
$expresion = new Expresion('if(bud.posa=2,1,0) AS proprietaire,if(bud.du=1 and bud.gog=1,1,0) as pa');
$query->select($expresion)->from('bud');
Related
I want use but not work please help me.
$sql = Yii::$app->db("INSERT INTO posts(name) VALUES(:name)");
$sql->bindValues([':name' => 'John']);
$sql->execute();
try create command exceute
Yii::$app->db->createCommand('INSERT INTO posts(name) VALUES(:name)')
->bindValues([':name' => 'John'])->execute();
I get a SQL in laravel like this
$scheduleMap=array("schedule_id"=>$scheduleId);
$scheduleConsume=DB::connection("mysql_report")->table("xxx")
->where($scheduleMap)
->whereBetween(DB::Raw("TO_DAYS(FROM_UNIXTIME(report_time))"),array(DB::Raw("TO_DAYS('".$startDate."')"),DB::Raw("TO_DAYS('".$startDate."')")))
->select(DB::Raw("SUM(imp_num) as imp_num"))
->first();
and I printed SQL as
select SUM(imp_num) as imp_num from `xxx`
where TO_DAYS(FROM_UNIXTIME(report_time))
between TO_DAYS('2016-11-05')
and TO_DAYS('2016-11-12')
Then I try to run sql in navicat,it works well,but in laravel,it return null.
I can't understand! I have to use whereRaw now.
Try this :)
$scheduleConsume = DB::connection("mysql_report")
->table('xxx')
->where('schedule_id', $scheduleId)
->whereBetween(
DB::Raw("TO_DAYS(FROM_UNIXTIME(report_time))"), [
DB::Raw("TO_DAYS('".$startDate."')"),DB::Raw("TO_DAYS('".$startDate."')")
]
)
->select(DB::Raw("SUM(imp_num) as imp_num"))
->first();
I use this for query not in:
$usertypes=Usertype::find()->where(['not in ','user_type_id',['2,3,4']])->all();
Error:
Database Exception – yii\db\Exception
Undefined offset: 1
Failed to prepare SQL: SELECT * FROM usertype WHERE user_type_id NOT IN :qp0
also tried the array format as ['2','3','4'] but not works?What is the issue?
Try This :
$usertypes=Usertype::find()
->where(['not in','user_type_id',[2,3,4]])
->all();
OR :
$usertypes=Usertype::find()
->where(['NOT',['user_type_id'=>[2,3,4]]])
->all();
Refer : http://www.bsourcecode.com/yiiframework2/select-query-model/#In-Condition
Maybe remove space character from 'not in '?
$usertypes=Usertype::find()->where(['not in', 'user_type_id', ['2,3,4']])->all();
you can use "not" word or "<>"
$usertypes=Usertype::find()->where(['not',['user_type_id'=>['2,3,4']]])->all();
or this
$usertypes=Usertype::find()->where(['<>',['user_type_id'=>['2,3,4']]])->all();
Try to use ->andFilterWhere instead of where ->where
Try this:
$usertypes = Usertype::find()
->andFilterWhere(['NOT IN','user_type_id',[2,3,4]])
->all();
$array=array(
'amount'=>$this->input->post('netamt'),
'trandate'=>$billdate,
'crcode'=>$this->input->post('rcode')
);
$this->db->where('voucher',$this->input->post('billno'));
$this->db->like('code','16','after');
$this->db->update('tranachst',$array);
when display this query using
echo $this->db->last_query();
UPDATE `tranachst` SET `amount` = '717360', `trandate` = '2015-07-15', `crcode` = '311001' WHERE `voucher` = '15020'
here like query not working , why ?
Please Try This --
My Exmaple --
$this->db->where('faq_id', $id);
$this->db->where('question LIKE ', '%do%');
$this->db->update('admin_faqs', $data);
Your Example --
$array=array(
'amount'=>$this->input->post('netamt'),
'trandate'=>$billdate,
'crcode'=>$this->input->post('rcode')
);
$this->db->where('voucher',$this->input->post('billno'));
$this->db->where('code LIKE ', '16%');
$this->db->update('tranachst',$array);
In this code you don`t need to use after, before parameter please try this..
Some people have the same question, and until now i don't know why. but clearly CI didn't support active record like above.
you can use $this->db->query() or
function index(){
$this->load->database();
$data=array(
'userlogin'=>'test'
);
$this->db->where('userlogin','xx');
$this->db->where("email like 'xx'");
$q=$this->db->update('master_user',$data);
}
something like that work too.
the same question : CodeIgniter Active Record 'like' ignoring 'where'
It seems that Active Record (Codeigniter) doesn't accept the WEEK parameter and I don't understand why?!
Whern i remove '3', my query works properly!
$this->db->select('WEEK(insere_le,3) AS semaine, COUNT(*) AS nombre')
->from($this->table_name)
->where(array(
'type_anomalie' => "Dérogation",
'YEAR(insere_le)' => $year
))
->group_by('WEEK(insere_le,3)')
->get()
->result_array();
This query, shows the following string when I execute it:
SELECT WEEK(insere_le, `3)` AS semaine, COUNT(*) AS nombre FROM (`aero_anomalie_montage`) WHERE `type_anomalie` = 'Dérogation' AND YEAR(insere_le) = '2014' GROUP BY WEEK(insere_le, `3)`
As you can see it adds an apostrophe before the number 3 and after the parenthesis )
It looks a little confused by your syntax. Why not put them in two different selects (if that's possible...)?
I don't know if this will help, but I found this, it will prevent it from adding the backticks altogether.
$this->db->select() accepts an optional second parameter. If you set
it to FALSE, CodeIgniter will not try to protect your field or table
names with backticks. This is useful if you need a compound select
statement.
http://ellislab.com/codeigniter/user-guide/database/active_record.html
hmm, if you look at active rec class (system/core/database/DB_active_rec.php) you will find this in function select() :
if (is_string($select))
{
$select = explode(',', $select);
}
so what you're select is doing is actually exploding your string into an array like this:
array(
WEEK(insere_le,
3) AS semaine,
COUNT(*) AS nombre
);
and then processing this.
This looks like an oversight from the developers of the active record class, and probably won't be solved by not escaping the values...
On the other hand, it actually checks its a string prior to the above.. so could try this:
$this->db->select(array('WEEK(insere_le,3) AS semaine', 'COUNT(*) AS nombre'))...
and the same with group_by(array('WEEK(insere_le,3)'))...
so end result would be:
$this->db->select(array('WEEK(insere_le,3) AS semaine', 'COUNT(*) AS nombre'))
->from($this->table_name)
->where(array(
'type_anomalie' => "Dérogation",
'YEAR(insere_le)' => $year
))
->group_by(array('WEEK(insere_le,3)'))
->get()
->result_array();
you tried this?:
$this->db->select("WEEK(insere_le,3) AS semaine, COUNT(*) AS nombre", FALSE);