I need to give my Angular JS application a JSON which represents Parent-Child Relationship of to tables.
Parent(Group):
+----+---------------+-------------+----------------+
| id | external_code | supplier_id | notes |
+----+---------------+-------------+----------------+
| 19 | asdfas | 3 | sadfa |
| 23 | 454 | 1 | groupa1 |
| 24 | sadfas221 | 2 | asfd |
| 25 | dsafas | 2 | NULL |
| 21 | 4545 | 1 | asdfasf |
+----+---------------+-------------+----------------+
Child(GroupItems):
+----------+---------+--------+
| group_id | item_id | status |
+----------+---------+--------+
| 19 | 1 | 0 |
| 19 | 2 | 0 |
| 19 | 3 | 0 |
| 25 | 2 | 0 |
+----------+---------+--------+
My desired JSON should look like this:
[
{"groupId":"19",
"notes":"sadfa",
"extenalCode":"asdfas",
"supplierId":"2",
"itemCount":3
"items":[{"itemId": "1","status":"Created", "weight":23},
{"itemId": "2","status":"Created", "weight":23}
{"itemId": "3","status":"Created", "weight":23}
]
},
....
]
The question is how to insert and bind child items with parent representing JSON semantics using MySQL and PlayFramework2.0(Slick)?
roughly something like this:
val items =
GroupItems.join(Items).on(_.itemId === _.id).run // <- query fetching items with group_ids
.groupBy(_._1.groupId).toMap
.mapValues(_._2) // <- mapping Map values to only items
// render groups to json and add a field items with the items (I may be wrong about Play's json api names)
val json = Group.run.map(g => Json.toJson(g) ++ JsObject("items" -> Json.toJson(items(g.id))))
Related
From a table with a column with json array of dictionaries i need to extract all values of key "user_id" one per row. If null or empty array return NULL. Similar to python pandas explode method.
Length of array is unknown.
Original table:
| id | users |
|----|-----------------------------------------------------------------|
| 1 |[{"id": 2, "mail": "u1#ab.com"}, {"id": 3, "mail": "u2#ab.com"}] |
| 2 |[{"id": 5, "email": "user3#hi.com"}]" |
| 3 | []
|
Processed table:
| id | users |
|----|----------|
| 1 | 2 |
| 1 | 3 |
| 2 | 5 |
| 3 | NULL |
select id, j.user_id from mytable left outer join
json_table(users, '$[*]' columns (user_id int path '$.user_id')) as j on true;
+------+---------+
| id | user_id |
+------+---------+
| 1 | 2 |
| 1 | 3 |
| 2 | 5 |
| 3 | NULL |
+------+---------+
Read https://dev.mysql.com/doc/refman/8.0/en/json-table-functions.html for more information on the JSON_TABLE() function.
Yii2 provides ActiveRecord::findBySql für raw SQL queries:
public static yii\db\ActiveQuery findBySql ( $sql, $params = [] )
Since there is no hint in the documentation: How to specify $params?
UPDATE
The reason there isn't any docs about the params passed to the findBySql() method is because the method returns the instance of ActiveQuery and if you see the last line of this method in yii\db\ActiveRecord.php it sets the $params via $query->params($params), means the yii\db\ActiveQuery function params($params) which defines the $params as
$params list of query parameter values indexed by parameter
placeholders. For example, [':name' => 'Dan', ':age' => 31].
I guess you should try the following way if lets say you have a table with name product
+----+-----------------+------------+--------+
| id | name | is_deleted | price |
+----+-----------------+------------+--------+
| 1 | shugal | 1 | 65.00 |
| 2 | spoon | 1 | 55.00 |
| 4 | kettle | 1 | 15.00 |
| 5 | spoon | 0 | 15.00 |
| 6 | plates | 0 | 105.00 |
| 7 | dishes | 0 | 15.00 |
| 8 | forks | 0 | 15.00 |
| 10 | pressure cooker | 0 | 203.00 |
| 16 | shugal | 1 | 65.00 |
| 17 | something | 0 | 25.00 |
| 25 | multi product | 0 | 0.00 |
| 66 | pans | 0 | 15.00 |
+----+-----------------+------------+--------+
using the following code you can select all the products that are deleted using params
$q = Product::findBySql(
"SELECT * FROM product where is_deleted=:deleted",
[':deleted' => 1]
)->all();
Hope this helps
I've got a MySQL database containing three tables. The database contains information about various electrical and mechanical components. It has three tables.
Tables:
componentSource - contains information about where the information in the database was sourced from.
component - contains part number information, description, etc. Multiple entries will refer to a single entry in the componentSource table as its source (Each source file describes multiple components).
componentParams - contains parametric information about the components. Multiple parameter entries will refer to a single entry the component table (each component has multiple parameters).
See simplified example tables...
Database Tables and Relationships:
+-------------------------------+
| Table: componentSource |
+-------------------------------+
| compSrcID* | sourceFile |
+-------------------------------+
| 1 | comp1.txt |
| 2 | comp2.txt |
| 3 | comp3.txt |
+-------------------------------+
^
|
+---------------------------------------------------+
( many to one reference) |
^
^
+---------------------------------------------------------------+
| Table: component |
+---------------------------------------------------------------+
| compID* | partNum | mfrPartNum | mfr | compSrcID |
+---------------------------------------------------------------+
| 1 | 1234 | ABCD | BrandA | 1 |
| 2 | 2345 | BCDE | BrandB | 1 |
| 3 | 3456 | CDEF | BrandC | 3 |
| 4 | 4567 | DEFG | BrandD | 2 |
+---------------------------------------------------------------+
^
|
+---------------+ (many to one reference)
|
^
^
+-------------------------------------------------------+
| Table: componentParams |
+-------------------------------------------------------+
| compParamID* | compID | paramName | paramValue |
+-------------------------------------------------------+
| 1 | 1 | ParamA | 50 |
| 2 | 1 | ParamB | 123 |
| 3 | 1 | ParamC | 10% |
| 4 | 1 | ParamD | 0.5 |
| 5 | 1 | ParamE | Active |
| 6 | 2 | ParamA | 25 |
| 7 | 2 | ParamB | 10K |
| 8 | 2 | ParamC | 5% |
| 9 | 2 | ParamD | 0.25 |
| 10 | 2 | ParamE | Proto |
| 11 | 3 | ParamA | 53.6 |
| 12 | 3 | ParamE | Active |
| 13 | 4 | ParamY | 123-56 |
| 14 | 4 | ParamZ | True |
+-------------------------------------------------------+
I would like to create a view of the database that merges information from the three tables. I would like to have a row for each line in the component table that merges the relevant lines from the componentSource table, and all of the relevant parameters out of the componentParams table.
See example view...
Database View:
+-------------------------------------------------------------------------------------------------------------------------------------------------------+
| View: componentView |
+-------------------------------------------------------------------------------------------------------------------------------------------------------+
| compID* | partNum | mfrPartNum | mfr | SourceFile | ParamA | ParamB | ParamC | ParamD | ParamE | ParamY | ParamZ |
| 1 | 1234 | ABCD | BrandA | comp1.txt | 50 | 123 | 10% | 0.5 | Active | | |
| 2 | 2345 | BCDE | BrandB | comp1.txt | 25 | 10K | 5% | 0.25 | Proto | | |
| 3 | 3456 | CDEF | BrandC | comp3.txt | 53.6 | | | | Active | | |
| 4 | 4567 | DEFG | BrandD | comp2.txt | | | | | | 123-56 | True |
+-------------------------------------------------------------------------------------------------------------------------------------------------------+
Since I want a line in the view for each component in the component table, I think merging the info from the componentSource table is fairly straight forward with a join, but the tricky part is creating columns in the view that correspond to the value in componentParam.paramName column. Seems like this requires some recursion to read all parameters associated with a component. Also note that not all components have all the same parameters in the parameter table, so the values for the parameters not used by a component would be null.
An alternative to creating a view, if that can't be done, would be to build another database table.
My SQL skills are super rusty, and were probably not up to this task when they were fresh.
Is it possible to create a view that creates columns that are based on row data (paramName) in a table? Could you show an example?
If not, can a table be built that does the same? Again, could you show an example?
Many thanks.
Conditional aggregation can do the pivoting for you
SELECT cp.compID,
ct.partNum,
ct.mfrPartNum,
ct.mfr,
cs.SourceFile,
MAX(CASE WHEN cp.paramName = 'ParamA' THEN cp.ParamValue END) as ParamA,
MAX(CASE WHEN cp.paramName = 'ParamB' THEN cp.ParamValue END) as ParamB,
MAX(CASE WHEN cp.paramName = 'ParamC' THEN cp.ParamValue END) as ParamC,
MAX(CASE WHEN cp.paramName = 'ParamD' THEN cp.ParamValue END) as ParamD,
MAX(CASE WHEN cp.paramName = 'ParamE' THEN cp.ParamValue END) as ParamE,
MAX(CASE WHEN cp.paramName = 'ParamY' THEN cp.ParamValue END) as ParamY,
MAX(CASE WHEN cp.paramName = 'ParamZ' THEN cp.ParamValue END) as ParamZ
FROM componentParameters cp
JOIN component ct ON cp.compId = ct.compId
JOIN componentSource cs ON cs.compSrcID = ct.compSrcID
GROUP BY cp.compID,
ct.partNum,
ct.mfrPartNum,
ct.mfr,
cs.SourceFile
It is also possible to use subqueries for this, however, I guess this should do the job better.
I would like to calculate average from nested relationship between eloquent models. So, let's say, I have 3 tables called programs, activities and statistics.
For simplicity sake, I will try to minimize the structure as follows:
program table:
-------------
| id | name |
-------------
| 1 | Foo |
| 2 | Bar |
-------------
activities table:
-----------------------------------
| id | program_id | name |
-----------------------------------
| 1 | 1 | Foo 1 |
| 2 | 1 | Foo 2 |
| 3 | 1 | Foo 3 |
| 4 | 2 | Bar 1 |
| 5 | 2 | Bar 2 |
-----------------------------------
statistics table:
-----------------------------------
| id | activity_id | type | score |
-----------------------------------
| 1 | 1 | A | 25 |
| 2 | 1 | B | 20 |
| 3 | 1 | A | 22 |
| 4 | 2 | A | 27 |
| 5 | 2 | B | 24 |
| 6 | 3 | A | 23 |
-----------------------------------
Now, what I want to get is the average of score of a program with specific type of statistic. I defined relationship in models, and tried following code, but no avail:
$program = Program::find(1);
$avg = $program->activities->where('statistics.type', 'A')->avg('statistics.value');
$avg always 0 or null if there is no activities in program, even without where clause.
i'm sure that i defined the relationship correctly because $program->activities returns a sets of activities and $activity-> statistics return a sets of statistics as well.
Any ideas?
You can use whereHas() like this:
Statistics::whereHas('activity', function ($q) use($programId) {
$q->where('program_id', $programId);
})
->where('type', 'A')
->avg('score');
Make sure you've defined activity relationship which should be "statistics belongsTo() activity".
I have a database which contain a list of items (item)
+----+-----------------------+-------+--------+-------+
| id | name | width | height | depth |
+----+-----------------------+-------+--------+-------+
| 1 | Some_toy | 35 | 28 | 30 |
| 2 | Pepperoni_pizza | 17 | 30 | 35 |
| 3 | Wood_table | 45 | 42 | 57 |
| 4 | Guitar | 26 | 45 | 75 |
| 5 | Awesome_TV | 80 | 35 | 10 |
+----+-----------------------+-------+--------+-------+
On my website, users can select items inside this list to create their own list of item. User's list are stored in the following table user_list_items
+----+--------------+---------+--------+
| id | id_user_list | id_item | number |
+----+--------------+---------+--------+
| 1 | 6 | 3 | 2 |
| 2 | 6 | 5 | 1 |
| 3 | 6 | 7 | 5 |
| 4 | 7 | 7 | 3 |
| 5 | 9 | 3 | 1 |
+----+--------------+---------+--------+
But, because my list cannot contain every item in the world, I want to allow users to add a custom item.
My problem is that I don't want to store users custom items inside my table item because this table contains the item displayed by default.
However, If I don't save custom items inside my table item, I still must save them somewhere else. So I've created another table custom_item which is an exact replica of item.
With this solution, I've added a foreign key id_custom_item in my table user_list_items which give the following
+----+--------------+---------+----------------+--------+
| id | id_user_list | id_item | id_custom_item | number |
+----+--------------+---------+----------------+--------+
| 1 | 6 | 3 | 0 | 2 |
| 2 | 6 | 0 | 25 | 1 |
| 3 | 6 | 7 | 0 | 5 |
| 4 | 7 | 7 | 0 | 3 |
| 5 | 9 | 3 | 0 | 1 |
+----+--------------+---------+----------------+--------+
And now I have a problem with user_list_items because can link to item or to custom_item and it seems like a very bad practice.
My question is, how could I save user's custom input with a list of predetermined item ?
Sorry this question is not very well formulated but I couldn't find any other way to express it.
If the objection to storing custom items in item table is just that the contents are displayed by default, I'd add a column set to 'Y' or 'N' to the table and add that column to the WHERE clause of the query that populates the default list, eg AND default_flag = 'Y'