How to make django queryset ignoring white space - django

There's some stored in db. the column data can include one more white space like below.
Printer
-----------------------------------
No | name | data
-----------------------------------
1 | 3D Printer | 1
2 | 3d printer | 21
3 | 3dPrinter | 3
I want to select all of '3d printer'.
Tell me the way for this.

You can do like:
Printer.objects.extra(where=["LOWER(REPLACE(name,' ','')) = '3dprinter'"])
The obove query will first remove any space in the name and then make it lower case, next compare it with 3dprinter
Since all space in name is removed including the one after 3d, we need to compare name with just 3dprinter

I guess django doesn't support SQL REPLACE option for strings. But you can use raw sql. Here is Django doc regarding the same: https://docs.djangoproject.com/en/1.8/topics/db/sql/
And here is raw SQL query for ignoring whitespaces Query that ignore the spaces
In my opinion you should add another column in your table slug-name which will stored name without whitespaces. This way you can easily use Django ORM on your table.

Related

DynamoDB get items with an attribute that is a substring of a value

I'm trying to query items in DynamoDB checking if one of their attributes is a substring of a provided value.
So basically, I am trying to do a query using something similar to contains(attribute_name, :input_value) but like this instead: contains(:input_value, attribute_name) (check if the attribute is inside the input value)
Ex:
If my items have the following attributes:
---------------------
item_id | domain
---------------------
1 | amazon.com
---------------------
2 | amazon.com
---------------------
3 | google.com
---------------------
And I use 'https://us-east-1.console.aws.amazon.com/' as input I want the 2 first items to be returned from the query.
Is there a way to make something like this in DynamoDB?
No, you cannot do a substring match in reverse. Before making the request to DynamoDB you should remove only the domain portion from the string as thats what you want to evaluate on.

DynamoDB Unique list of elements across all records

I have a simple table that stores list of names for a particular record. I want to ensure that a name can never be used for any other record more than once. The names column should also not be empty; there should always be at least 1 given name.
| ID | Names |
|-----------------------|------------------------------------------------|
| 111 | [john, bob] |
| 222 | [tim] |
| 333 | [bob] (invalid bob already used) |
Easiest solution I believe for this case is to simply use a 2nd table for the values you are interested in being the primary key. Then in application code, simply check that new table if they exist or not to determine if you should create a new record in the primary table. For a List[L] this simply avoids having to traverse every single list of every record to determine if a particular scalar value already exists or not. Credit to Tamas.
https://advancedweb.hu/how-to-properly-implement-unique-constraints-in-dynamodb/
Here’s a blog post describing how to best enforce uniqueness constraints: https://aws.amazon.com/blogs/database/simulating-amazon-dynamodb-unique-constraints-using-transactions/

Fastest way to search a varchar column in MySQL

I want to implement a search query for a bookshop. I use MySQL and I have a varchar column which contains name, author or other details like The Tragedy of Hamlet, Prince of Denmark, by William Shakespeare and I want to search like shakespeare tragedy or denmark tragedy to have a list of books have them in their one column.
I have three queries to implement this but I want to know about their performance.
LIKE %%
my first way is to split search text into words and create a dynamic command based on word counts:
SELECT * FROM books
WHERE name LIKE '%shakespeare%'
AND name LIKE '%tragedy%'
But I was told that like is a slow operator specially with two % because it can not use index.
TAG table and relational division
My second way is to have another table which contains tags like:
-------------------------
| book_id | tag |
|-----------------------|
| 1 | Tragedy |
| 1 | Hamlet |
| 1 | Prince |
| 1 | Denmark |
| 1 | William |
| 1 | Shakespeare |
-------------------------
And create a dynamic divide command:
SELECT DISTINCT book_id FROM booktag AS b1
WHERE ((SELECT 'shakespeare' as tag UNION SELECT 'tragedy' as tag)
EXCEPT
SELECT tag FROM booktag AS b2 WHERE b1.book_id = b2.book_id) IS NULL
But I was told that relational division is so slow too.
REGEXP
My third way is to use regular expressions:
SELECT * FROM books
WHERE name REGEXP '(?=.*shakespeare)(?=.*tragedy)'
But someone told me that it is slower than LIKE
Please help me decide which way is faster?
Surely using LIKE which is a built-in operand, is more optimized than Regular expression. But there is an important point here that you can not compare these two recipes together, because LIKE used to add a wildcard to string and regex is for matching a string based on a pattern which can be much complex.
Anyway the best ways which come in my mind for this aim, would be one of the followings:
Use LIKE on your column which has been indexed properly.1
Using some optimized search technologies like elastic search.
Implement a multithreading algorithm 2 which performs very good with IO tasks. For this one you can use some tricks like defining an offset and divide the table among the threads.
Also for some alternative ways read this article https://technet.microsoft.com/en-us/library/aa175787%28v=sql.80%29.aspx
1. You should be careful of the way you put the indices on your columns.read this answer for more info https://stackoverflow.com/a/10354292/2867928 and this post http://use-the-index-luke.com/sql/where-clause/searching-for-ranges/like-performance-tuning
2.Read this answer for more info Multi Thread in SQL?

SQL Where between word value ranges (eg., "low" to "high")

I have a field in my database that has 5 possible values: fair, good, very good, ideal, siganture ideal
I have a coldfusion form that has 2 drop-downs each with all the values. What I am looking to do is be able to have the user select a range. For example dropdown1 = Fair dropdown2 = Very Good. So this would somehow generate the SQL WHERE statement:
grade IN ('fair', 'good', 'very good')
Can you think of a smart way to program this given that the values have to be this way. I think maybe if I put them in an array and then looped through it or something. I'm a little stumped on this any help would be appreciated.
As others mentioned, redesigning is ultimately the better course of action, both in terms of efficiency and data integrity. However, if you absolutely cannot change the structure, a possible workaround is to create a lookup table of the allowable grade descriptions, along with a numeric rating value for each one:
GradeID | GradeText | Rating
1 | Fair | 0
2 | Good | 1
3 | Very Good | 2
4 | Ideal | 3
5 | Signature Ideal | 4
Then populate your select list from a query on the lookup table. Be sure to ORDER BY Rating ASC and use the rating number as the list value. Then on your action page, use the selected values to filter by range. (Obviously validate the selected range is valid as well)
SELECT t.ColumnName1, t.ColumnName2
FROM SomeTable t INNER JOIN YourLookupTable lt ON lt.Grade = t.GradeText
WHERE lt.Rating BETWEEN <cfqueryparam value="#form.dropdown1#" cfsqltype="cf_sql_integer">
AND <cfqueryparam value="#form.dropdown2#" cfsqltype="cf_sql_integer">
Again, I would recommend restructuring instead. However, the above should work if that is really not an option.

Is it possible to create a mediawiki table where the columns are filled in by multiple templates?

Wikimedia explains how you can create a table based on multiple templates, where each row is formed from a single template.
Is there anyway to do the same for columns? It sounds easy but I could not get it to work. Instead my page displayed multiple tables instead of multiple columns.
Here's some code:
Row page
{|class="wikitable sortable" style="font-size:85%"
|+ table name
! Title 1
! Title 2
! Title 3
|-
{{R1}}
{{R2}}
{{R3}}
|}
Row template:
| Row 1, Column 1
| Row 1, Column 2
| Row 1, Column 3
|-
Column page
{{C1}}{{C2}}{{C3}}
Column template:
Don't know how this should work.
The only way I know of, other than creating a virtual table with floating <div>s, is using Labeled Section Transclusion (or its equivalent {{#dpl}} function if you have Extension:DynamicPageList). You separately label each cell of your "column template" using <section/> tags or headings, and then create a framework table that fills in each row by transcluding from the column templates. The structure would look something like this:
{|class="wikitable sortable" style="font-size:85%"
|+ table name
! Title 1
! Title 2
! Title 3
|- valign="top
| {{#lst:col1|row1}}
| {{#lst:col2|row1}}
| {{#lst:col3|row1}}
|- valign="top
| {{#lst:col1|row2}}
| {{#lst:col2|row2}}
| {{#lst:col3|row2}}
|- valign="top
| {{#lst:col1|row3}}
| {{#lst:col2|row3}}
| {{#lst:col3|row3}}
...
|}
This solution lacks the simplicity of tossing out columns the way you can toss out rows, but sadly HTML just isn't designed to create true column-based tables. However, with this layout at least all the information from each column is stored on the same page where you can edit it more easily ({{col1}}, {{col2}}, etc.), and if you're recording textual information then you can also read each column as a distinct page which might be handy.
If you plan on doing this a lot, you can further simplify this by creating a template that fills in each row so you only need to specify the row number, something like this: (assuming your columns are always placed as subpages of the article where they will appear)
template:col-based table row
{{!}}- valign="top"
{{!}} {{#lst: {{PAGENAME}}/col1 | row{{{1|}}} }}
{{!}} {{#lst: {{PAGENAME}}/col2 | row{{{1|}}} }}
{{!}} {{#lst: {{PAGENAME}}/col3 | row{{{1|}}} }}
Then under the header you'd just need to list:
{{col-based table row|1}}
{{col-based table row|2}}
{{col-based table row|3}}
...
Now, if you want to automatically-generate these rows until you exhaust the template (or in other words, have the table determine how many rows to create without you manually setting them up), that I don't think I can help you with. Perhaps you can do something clever with the DPL extension's table generation function, or install an extension that will allow you to create loops, but I think you'll probably be stuck creating each row manually as described above.
As a final note, because this is content and not formatting I'd advise that you put the column pages in the mainspace rather than the template namespace. In fact, this is the sort of thing that subpages are great for.
I think this is impossible. I thought about it for a bit, and I have no idea how to do it.
Maybe some sort of nested Templates that rearrange the order of the columns and rows. Seems very difficult, and I'm not sure it's theoretically possible.
you could get the columns filled by templates using DIVs, but you wil have to recreate and re-size each cell within each div manually