How to use whereBetween and like operator together in laravel? - mysql

I need to change the below MySQL code to laravel format using where, between and like operators together:
select count(id) from tbl1
where created_at BETWEEN
'2018-12-10%' AND '2018-12-12%'

I found the answer as below:
\DB::table('tbl1')->whereBetween('created_at',[$sdate.'%', $enddate.'%'])->count();

If you're using a Model you could use the whereBetween method, take a look at the following example:
User::query()->whereBetween('created_at', [$start, $end])->count();

Related

Convert SQL query to Query builder Laravel

I want to convert this SQL query to Query builder Laravel
SELECT * FROM articles ORDER BY (titre LIKE '%book%') DESC
Updated:
Try the following code:
DB::table('articles')->orderBy(DB::raw("title LIKE '%$value%'"),'desc')->get();
I think the following one also solves your problem:
DB::table('articles')->orderByRaw("(title LIKE '%book%') DESC")->get();

How to execute a MySQL function in a Knex query?

I have a BINARY field in my table which I usually grab like this:
SELECT HEX(users.id) AS id FROM users WHERE username = ?
I recently started using Knex because I need to be able to dynamically generate WHERE clauses from objects. Here's what I tried:
knex('users').select('HEX(users.id) AS id)').where(filter);
Here's the query it generates:
select `HEX(users`.`id)` as `id` ....
And then I tried this:
knex('users').select('HEX(`users`.`id`) AS id').where(filter);
And it comes up with this:
select `HEX(``users```.```id``)` as `id` ....
How do I execute HEX() without it being mistaken for a column name?
With knex letting to do quoting of identifiers it would look like this:
knex('users').select(knex.raw('HEX(??) AS id', ['users.id'])).where(filter);
I've found a solution. I have to use raw() function. So my query builder will look like this:
knex('users').select(knex.raw('HEX(`users`.`id`) AS id')).where(filter);

suggest an alternative query for this type of condition in SQL

suggest an alternative query for this type of condition in sql
select v_sdi_previous_id,v_sdi_settlement_flag,v_sdi_studentid
from schooldev."STUDENT_DETAILS_INFO"
where (upper(v_sdi_studentid)=upper('BS15B016') or (upper(v_sdi_previous_id)=('BS15B016')) )
and n_sdi_schoolid='1' and v_sdi_active_flag='Y'
It is pretty simple query. What alternative you want ?
You may use IN instead of multiple OR like-
where upper(v_sdi_studentid) IN ('BS15B016', 'BS15B016')
and n_sdi_schoolid='1' and v_sdi_active_flag='Y'.
Nothing much is needed with this query unless you are not getting the output as per your need.
select v_sdi_studentid as new_id,v_sdi_previous_id,v_sdi_settlement_flag from schooldev."STUDENT_DETAILS_INFO" where upper(v_sdi_previous_id) IN ('BS15B016') and n_sdi_schoolid='1' and v_sdi_active_flag='Y'

Comparing two fields in django queryset

How would I do the following SQL query in the Django ORM?
SELECT * FROM my_table WHERE date_added > date_created;
I'm guessing you don't have the date_created available as a variable (as #tttthomasssss assumes) so it will be something like this:
from django.db import models
YourTable.objects.filter(date_added__gt=models.F('date_created')
Docs on F expressions: https://docs.djangoproject.com/en/dev/ref/models/expressions/#f-expressions
You can use F() expressions in queries like this:
MyModel.objects.filter(date_added__gt=F('date_created'))
You can use __gt for >: MyModel.objects.filter(date_added__gt=date_created).
See the documentation for querysets here.
EDIT:
If date_created isn't available to you, then #Wolph's solution is the one.

How to use a LIKE query with CodeIgniter?

I want to run a query like this:
SELECT * FROM table WHERE field LIKE '%search_term%'
In CI you can bind parameters to queries, if you used field=? but this does not work for field LIKE "%?%". From debugging output it seems the query used is field LIKE "%'search'%".
Is there an alternative way to do searching in CodeIgniter?
You can use this query:
SELECT * FROM table WHERE field LIKE ?
And bind with %search% instead of search.
You should be aware that this query will be slow in MySQL. You might want to look at free-text search instead (Lucene, Sphinx, or MySQL's built-in free-text search functions).
this is active record class for codeigniter
$this->db->select('*');
$this->db->like('columnname','both');
$query=$this->db->get("tablesname");
$result=$query->result_array();
if(count($result))
{
return $result;
}
else
{
return FALSE;
}
You should try this..I think its help you..
both means %columnname%,
before means %columnname,
after means columnname%
$search_term=$this->input->post('textboxName');
$search_term="%".$search_term."%";
$sql="SELECT * FROM table WHERE field LIKE ? ";
$query=$this->db->query($sql,array($search_term));
$res=$query->result();
what i can understand CI is adding quotes, pass FALSE as third parameter while binding to prevent CI adding quotes.