Here is JSON data in a database column named "debts":
{
"debt_block_1":{
"debt_type_1":"House",
"repayment_frequency_1":"Monthly",
"financial_institution_1":"anz",
"repayment_amount_1":"899",
"amount_your_share_1":"98",
"loan_term_1":"3",
"rate_1":"3"
},
"debt_block_2":{
"debt_type_2":"Creditcard",
"repayment_frequency_2":"Monthly",
"financial_institution_2":"anz",
"credit_limit_2":"2000",
"remaining_balance_2":"200"
},
"special_notes":""
}
Here, I need to get debt types if financial institution is "anz". I have no experience with writing SQL queries for JSON data types.
I tried the following query:
select JSON_SEARCH(debts, 'all' , 'anz') from table_name;
and the result of the query is given below:
["$.debt_block_1.financial_institution_1", "$.debt...
Editing start
Another query I executed is
SELECT debts, JSON_EXTRACT(debts, "$.debt_type")
FROM application_data
WHERE JSON_EXTRACT(debts, "$.financial_institution") = 'anz'
No result.
Editing end
Any suggestions on how I could get the debt_type value where financial_institution ="anz"?
You would have an easier time finding the desired records if you were able to use JSON_CONATINS looking for 'finanical_instituion'. But having 'financial_instituion_1', 'financial_instituiton_2' means you have to search for all of those variants.
Related
I've created a MySQL sproc which returns 3 separate result sets. I'm implementing the npm mysql package downstream to exec the sproc and get a result structured in json with the 3 result sets. I need the ability to filter the json result sets that are returned based on some type of indicator in each result set. For example, if I wanted to get the result set from the json response which deals specifically with Suppliers then I could use some type of js filter similar to this:
var supplierResultSet = mySqlJsonResults.filter(x => x.ResultType === 'SupplierResults');
I think SQL Server provides the ability to include a hard-coded column value in a SQL result set like this:
select
'SupplierResults',
*
from
supplier
However, this approach appears to be invalid in MySQL b/c MySQL Workbench is telling me that the sproc syntax is invalid and won't let me save the changes. Do you know if something like what I'm trying to achieve is possible in MySQL and if not then can you recommend alternative approaches that would help me achieve my ultimate goal of including some type of fixed indicator in each result set to provide a handle for downstream filtering of the json response?
If I followed you correctly, you just need to prefix * with the table name or alias:
select 'SupplierResults' hardcoded, s.* from supplier s
As far as I know, this is the SQL Standard. select * is valid only when no other expression is added in the selec clause; SQL Server is lax about this, but most other databases follow the standard.
It is also a good idea to assign a name to the column that contains the hardcoded value (I named it hardcoded in the above query).
In MySQL you can simply put the * first:
SELECT *, 'SupplierResults'
FROM supplier
Demo on dbfiddle
To be more specific, in your case, in your query you would need to do this
select
'SupplierResults',
supplier.* -- <-- this
from
supplier
Try this
create table a (f1 int);
insert into a values (1);
select 'xxx', f1, a.* from a;
Basically, if there are other fields in select, prefix '*' with table name or alias
I'm trying to count distinct string values for a fitered set of results in a django query against a mysql database versus the same data in a postgres database. However, I'm getting really confusing results.
In the code below, NewOrder represents queries against the same data in a postgres database, and OldOrder is the same data in a MYSQL instance.
( In the old database, completed orders had status=1, in the new DB complete status = 'Complete'. In both the 'email' field is the same )
OldOrder.objects.filter(status=1).count()
6751
NewOrder.objects.filter(status='Complete').count()
6751
OldOrder.objects.filter(status=1).values('email').distinct().count()
3747
NewOrder.objects.filter(status='Complete').values('email').distinct().count()
3825
print NewOrder.objects.filter(status='Complete').values('email').distinct().query
SELECT DISTINCT "order_order"."email" FROM "order_order" WHERE "order_order"."status" = Complete
print OldSale.objects.filter(status=1).values('email').distinct().query
SELECT DISTINCT "order_order"."email" FROM "order_order" WHERE "order_order"."status" = 1
And here is where it gets really bizarre
new_orders = NewOrder.objects.filter(status='Complete').values_list('email', flat=True)
len(set(new_orders))
3825
old_orders = OldOrder.objects.filter(status=1).values_list('email',flat=True)
len(set(old_orders))
3825
Can anyone explain this discrepancy? And possibly point me as to why results would be different between postgres and mysql? My only guess is a character encoding issue, but I'd expect the results of the python set() to also be different?
Sounds like you're probably using a case-insensitive collation in MySQL. There's no equivalent in PostgreSQL; the closest is the citext data type, but usually you just compare lower(...) of strings, or use ILIKE for pattern matching.
I don't know how to say it in Django, but I'd see if the count of the set of distinct lowercased email addresses is the same as the old DB.
According to the Django docs something like this might work:
NewOrder.objects.filter(status='Complete').values(Lower('email')).distinct()
hi i am executing nested "select" query in mysql .
the query is
SELECT `btitle` FROM `backlog` WHERE `bid` in (SELECT `abacklog_id` FROM `asprint` WHERE `aid`=184 )
I am not getting expected answer by the above query. If I execute:
SELECT abacklog_id FROM asprint WHERE aid=184
separately
I will get abacklog_id as 42,43,44,45;
So if again I execute:
SELECT `btitle` FROM `backlog` WHERE `bid` in(42,43,44,45)
I will get btitle as scrum1 scrum2 scrum3 msoffice
But if I combine those queries I will get only scrum1 remaining 3 atitle will not get.
You Can Try As Like Following...
SELECT `age_backlog`.`ab_title` FROM `age_backlog` LEFT JOIN `age_sprint` ON `age_backlog`.`ab_id` = `age_sprint`.`as_backlog_id` WHERE `age_sprint`.`as_id` = 184
By using this query you will get result with loop . You will be able to get all result with same by place with comma separated by using IMPLODE function ..
May it will be helpful for you... If you get any error , Please inform me...
What you did is to store comma separated values in age_sprint.as_backlog_id, right?
Your query actually becomes
SELECT `ab_title` FROM `age_backlog` WHERE `ab_id` IN ('42,43,44,45')
Note the ' in the IN() function. You don't get separate numbers, you get one string.
Now, when you do
SELECT CAST('42,43,44,45' AS SIGNED)
which basically is the implicit cast MySQL does, the result is 42. That's why you just get scrum1 as result.
You can search for dozens of answers to this problem here on SO.
You should never ever store comma separated values in a database. It violates the first normal form. In most cases databases are in third normal form or BCNF or even higher. Lower normal forms are just used in some special cases to get the most performance, usually for reporting issues. Not for actually working with data. You want 1 row for every as_backlog_id.
Again, your primary goal should be to get a better database design, not to write some crazy functions to get each comma separated number out of the field.
I can do this in MySQL:
WHERE 1 AND 1 AND 1
How can i repeat it in MongoDB? What is MongoDB's equivalent for WHERE 1 ?
UPDATE:
So. I don't know how choose best answer ^^ and expanded question. As #mark-hillick noticed - i'm searching the best way to build query.
Now I'm using this way (express+mongoose):
//req.query - get/post object in Express
for (var q in req.query) {
if (req.query[q]) { //simplified example
query[q] = req.query[q];
};
}
Collection.find(query)
Your suggestions?
There is a SQL-MongoDB Mapping Chart here that you will find useful.
It has a tonne of examples on what you do within MongoDB when you want to do the same operation as "WHERE" in MySQL. For example -
SELECT a,b FROM users WHERE age=33
is
db.users.find({age:33}, {a:1,b:1})
or
SELECT * FROM users WHERE a=1 and b=1
is
db.users.find({a:1,b:1})
MongoDB is document oriented database and documents in MongoDB consists key-value pairs. So, in MongoDB you can't run single value query as you did in MySql. Assuming you hold your data in the field name a, similar query in MongoDB could be like :
db.test.find({$and : [{a:1},{a:1}, {a:1}]});
If' you're trying to build query clauses, AND is implicit in Mongo. Therefore, if you have the following;
db.col.find({name:"dave"})
you could just add another;
db.col.find({name:"dave", age:33})
and so on.
How do I convert the following into MongoDB query ?
sets_progress = Photo.select('count(status) as count, status, photoset_id')
.where('photoset_id IN (?)', sets_tracked_array)
.group('photoset_id, status')
There is no 1 to 1 mapping of a SQL query to a NoSQL implementation. You'll need to precalculate your data to match the way you want to access that data.
If it is small enough, then this query will need to change into a map-reduce job. More here: http://www.mongodb.org/display/DOCS/MapReduce
Here's a decent tutorial that takes a query that GROUP's and converts to map-reduce: http://www.mongovue.com/2010/11/03/yet-another-mongodb-map-reduce-tutorial/