How do I do mysql "LIKE" in django? - mysql

SELECT * from myapp_mymodel WHERE code LIKE "914%";

I would recommend reading the manual, it clearly tells you how to do this: http://docs.djangoproject.com/en/dev/ref/models/querysets/#startswith
MyModel.objects.filter(code__startswith='914')

Related

TUniTable/TFDTable.Filter wildcard limitation - like '[AB]*'

Please help me with filter option.
Is it possible to use the wildcard option Client like '[AB] *' for records that start with 'A' or 'B'? This option works in Access, but it does not work in Unidac Table or FireDac Table as Filter.
Solution is use FilterSQL, Unidac(8*), in Unidac 9* is this problem fixed (i think)
https://www.devart.com/unidac/revision_history.html
Client like '%p[o,ó]k[u,ú,ů][s,š]%'

How to use whereBetween and like operator together in laravel?

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();

How do I convert "7-1-2014" to "07-01-2014" in MySQL Workbench?

I have already gone through part of the process to convert my text to a proper date:
7-jan -> 7-1 -> 7-1-2014
(note: dd-mm-yyyy)
However, I am now required to change the 7-1-2014 format to 07-01-2014 in order to change the text to a date.
Is there an easy way to do this?
Am not too experienced but have a basic understanding of queries and functions.
you're looking for something like this :
select STR_TO_DATE('7-1-2017', '%d-%m-%Y')
Try this:
SELECT STR_TO_DATE('7-1-2014', '%e-%m-%Y');
SELECT STR_TO_DATE('7-Jan-2014', '%e-%M-%Y');

What is the "Rails Way" of doing a query with an OR clause using ActiveRecord?

I'm using Rails 3 with a MySQL database, and I need to programmatically create a query like this:
select * from table where category_name like '%category_name_1%'
OR category_name like '%category_name_2%'
(...snip...)
OR category_name like '%category_name_n%'
Given the table size and the project scope (500 rows at most, I think), I feel that using something like thinking sphinx would be overkill.
I know I could simply do this by writing the query string directly, but wanted to know if there's an ActiveRecord way to do this. There's no mention of this on the official guide, and I've been googling for a long while now, just to end empty-handed :(
Also, is there a reason (maybe a Rails reason?) to not to include the OR clause?
Thanks!
Assuming you have an array names with category names:
Model.where( names.map{"category_name LIKE ?"}.join(" OR "),
*names.map{|n| "%#{n}%" } )
you should google first, there is already an answer.
Look here and then here
and you'll get something like this:
accounts = Account.arel_table
Account.where(accounts[:name].matches("%#{user_name}%").or(accounts[:name].matches("%#{user_name2}%")))
If you look at the guide, they have examples that can easily be modified to this:
Client.where("orders_count = ? OR locked = ?", params[:orders], false)
Mysql has a regexp function now that can clean things up a bit, assuming there's no regex metachars in your category names:
Table.where "category_name regexp '#{names.join('|')}'"

How to write this kind of a mysql query?

I want to write this kind of a MySQL query.
select image_path, replace(replace(replace(description,\'/images/\',image_path),\'<![CDATA[\',\'\'),\']]>\',\'\') description from test_image_master where ...
If I echo this query from php it is showing like this
select image_path, replace(replace(replace(description,'/images/',image_path),'','') description from test_image_master where ...
Missing <![CDATA[ and ]]>. So the query is not working properly.
I am using php and mysql.
How can I solve this?
It won't miss. You just can't see from browser, you can check the source of html.