I am trying to combine Where And OR in Codeigniter via code.
$this->db->where("id",1);
$this->db->where("status","live")->or_where("status","dead");
Appeared result is as query
where id=1 and status='live' OR status='dead'
Whereas i need result for this query.
where id=1 and (status='live' OR status='dead');
NOTE: Please don't give solution of string passed as parameter from where function. That's not fit into my code. I Just simplified my problem above.
Just found solution, if it can help some one.
$this->db->where("id",1)->where("(status='live' OR status='dead')");
you can use where_in() like this
$status_array = array('live','dead');
$this->db->where("id",1);
$this->db->where_in('status', $status_array);
you just add ->group_start() and ->group_end()
$this->db->where("id",1);
$this->db->where("status","live")
->group_start()
->or_where("status","dead")
->group_end();
Related
I want to write this raw query using Eloquent. I have tried using ->whereRaw but it shows error due to special characters in it.
SELECT id,shipping_email FROM orders WHERE 1 AND `shipping_email` NOT REGEXP '^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,63}$' AND id = '<>'
Help is appreciated...
Thanks in advance.
I have got answer, I have tried these codes..
\Model\Order::query()
->select('id', 'shipping_email')
->whereRaw("`shipping_email` NOT REGEXP ?", ["^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,63}$"])
->get();
Hello There!
I want to get only the rows where the punch_in_time is greater than the expected_punch_in_time (You can see the columns in the picture link above).
I searched a lot and I did not found anything helpful, I tried the "whereDate" method but it turns out that it only works on datetime fields and not only time as in my case.
Any help would be appreciated!
Thank you.
You can use whereTime function in querybuilder
whereTime('punch_in_time', '>', 'expected_punch_in_time')
for more details refer link https://laravel.com/docs/9.x/queries
Use eloquent: whereTime (colum, '>' colum);
Or if want pure SQL u can use this:
SELECT * FROM testTime WHERE
CAST(punch_in_time AS TIME) > CAST(expected_punch_in_time AS TIME);
SELECT * FROM testTime WHERE
punch_in_time > expected_punch_in_time;
fiddle here
Finally, I figured it out, I have used the "whereColumn" method and it worked well.
public function ScopeLateDrivers($query)
{
return $query->whereColumn('punch_in_time', '>', 'expected_punch_in_time');
}
I have two columns in my table: max and current. I want to build simple scope
public function scopeNotMax($query)
{
return $query->where('max', '>', 'current');
}
But Laravel gives me that query:
SELECT * FROM table WHERE `max` > 'current'
I don't want this result and I know that I can use in this place whereRaw() or DB::raw(). But I can't find another way to say "hey, this is column, not string!'. Can I do it? Or I must use raws? I want to avoid it.
There is no other way.
where() method in this case add third parameter (value) to bindings and passes it ot PDO library. So it will be escaped.
Alternatively You can pass as third parameter a Closure, but then laravel will form a sub-select for You, which doesn't helps much.
Looks like whereRaw() is made for this kind of sitiuation.
Did you give a try with this ? return $query->where('max > current');
you can use whereRaw():
->whereRaw('table_1.name = table_2.name');
You exmaple code:
->whereRaw('max>current');
I have query that fetch data...
I want to order it by absolute value,
current query:
$someVar = DB::table('tblName')->where('id', $id)->orderBy('size', 'desc')->get
I tried to do something like orderBy(Abs('size'), 'desc')->get
but it didn't work and can't find any solution in the documentations.
Anyone know the solution?
You need to use the method raw for this to work.
->orderBy(DB::raw('ABS(size)'), 'desc');
I want to count my results of an active record query with CI (using postgreSQL). I am using count_all_results(), which works fine for most queries, but not when using distinct() or group_by() after a join, because count_all_results() ignores these functions.
Here is a fix vor this, which is not yet implemented in the newsest (2.1.3) stable version.
https://github.com/EllisLab/CodeIgniter/commit/b05f506daba5dc954fc8bcae76b4a5f97a7433c1
When I try to implement this fix in the current version myself, there is no additional filtering done. The row count stays the same.
Any tips on how to implement this in the current version, or other ways to count results filtered by distinct() or group_by()?
$this->db->select('COUNT(id)');
$this->db->join();
$this->db->distinct();
$this->db->group_by();
//...etc ...
$query = $this->db->get('mytable');
return count($query->result());
or
$this->db->join();
$this->db->distinct();
$this->db->group_by();
//...etc ...
$query = $this->db->get('mytable');
return $query->num_rows();
You can use
$this->db->group_by();
otherwise
$this->db->distinct();
In my case, using Codeigniter 3.1.11 as of today, distinct() is taken into account when doing count_all_results(). HOWEVER you have to use the function, doing a $this->db->select("distinct x") and then the counting, won't apply the DISTINCT limitation for the record count.