Split SQL Table with fields containing JSON into multiple rows - json

I have a table t with 2 fields: one containing an id, and the other one containing a json.
My table looks like this:
| id | json |
|:---|:----------------------------------------------|
| 1 | {"tag1":45, "tag2": 3, "tag5": 10} |
| 2 | {"tag5":35, "tag6": 7, "tag8": 10, "tag10": 4}|
| 3 | {"tag2":10, "tag800": 6} |
I am trying to write a postgresql query to create a table that looks like the following but I am stuck:
| id | key | Value |
|:---|:--------|:------|
| 1 | tag1 | 45 |
| 1 | tag2 | 3 |
| 1 | tag5 | 10 |
| 2 | tag5 | 35 |
| 2 | tag6 | 7 |
| 2 | tag8 | 10 |
| 2 | tag10 | 4 |
| 3 | tag2 | 10 |
| 3 | tag800 | 6 |
Note that there are thousands of different keys in my data.
Any help would be much appreciated. Thanks!

Recreating your example with
CREATE TABLE test(id int, mydata jsonb);
insert into test values (1, '{"tag1":45, "tag2": 3, "tag5": 10}');
insert into test values (2, '{"tag5":35, "tag6": 7, "tag8": 10, "tag10": 4}');
insert into test values (3, '{"tag2":10, "tag800": 6} ');
You can achieve what you're looking for with the jsonb_each function
select id, key, value from test, jsonb_each(test.mydata)

Related

Mysql explode json array to rows

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.

Is there a way in MySQL to update a view with data from a table that is not covered by the view?

I have two tables, test1 and test2:
test1
| id | name |
| -- | ----- |
| 1 | Steve |
| 2 | Phil |
| 3 | Tony |
test2
| id | name |
| -- | ----- |
| 4 | Peter |
| 5 | Perry |
I have created a view from test1 using the code
CREATE VIEW test_view AS SELECT * from test1;
I wish to update this test_view with data from test2.
What I want is as shown below
| id | name |
| -- | ----- |
| 1 | Steve |
| 2 | Phil |
| 3 | Tony |
| 4 | Peter |
| 5 | Perry |
I am hoping to find a way to do this without joining the two tables.
You can use a union.
create table a (id int, name varchar(255));
create table b (id int, name varchar(255));
insert into a values (1, "c"), (2, "d");
insert into a values (5, "zz"), (6, "ff");
insert into b values (1, "a"), (2, "b");
create view c as select * from a union select * from b;
select * from c;
+------+------+
| id | name |
+------+------+
| 1 | a |
| 2 | b |
| 1 | c |
| 2 | d |
| 5 | zz |
| 6 | ff |
+------+------+
As per Solarflare -
A view is just the result of a query. The "content" of a view changes
when you change the underlying data (e.g. table test1) - and it
doesn't change if you don't change the underlying data
So, when I perform, test1 = test1.append(test2), the test_view that I have created from the underlying test1 dataframe gets updated automatically.

How to sum the values of column on duplicate key?

I have a table like this:
// mytable
+----+--------+-------+-------+
| id | name | key | value |
+----+--------+-------+-------+
| 1 | jack | 1 | 10 |
| 2 | peter | 1 | 5 |
| 3 | jack | 2 | 5 |
| 4 | ali | 1 | 2 |
| 5 | jack | 1 | 5 |
| 6 | jack | 1 | 10 |
| 7 | bert | 4 | 2 |
| 8 | peter | 2 | 10 |
| 9 | bert | 4 | 5 |
+----+--------+-------+-------+
Now I want to sum the numbers of value where both name and key are identical. So, I want this output:
// mynewtable
+----+--------+-------+-------+
| id | name | key | value |
+----+--------+-------+-------+
| 1 | jack | 1 | 25 |
| 2 | peter | 1 | 5 |
| 3 | jack | 2 | 5 |
| 4 | ali | 1 | 2 |
| 7 | bert | 4 | 7 |
| 8 | peter | 2 | 10 |
+----+--------+-------+-------+
Is it possible to I do that?
Edit: How can I do that for insert?
// mytable
+----+--------+-------+-------+
| id | name | key | value |
+----+--------+-------+-------+
| 1 | jack | 1 | 25 |
| 2 | peter | 1 | 5 |
| 3 | jack | 2 | 5 |
| 4 | ali | 1 | 2 |
| 7 | bert | 4 | 7 |
| 8 | peter | 2 | 10 |
+----+--------+-------+-------+
Inserting these rows:
+----+--------+-------+-------+
| 10 | jack | 1 | 5 |
+----+--------+-------+-------+
+----+--------+-------+-------+
| 11 | bert | 1 | 2 |
+----+--------+-------+-------+
What I want: (output)
// mynewtable
+----+--------+-------+-------+
| id | name | key | value |
+----+--------+-------+-------+
| 1 | jack | 1 | 30 |
| 2 | peter | 1 | 5 |
| 3 | jack | 2 | 5 |
| 4 | ali | 1 | 2 |
| 7 | bert | 4 | 7 |
| 8 | peter | 2 | 10 |
| 11 | bert | 1 | 2 |
+----+--------+-------+-------+
You have to group by more columns.
select name, key, sum(value) from mytable group by name, key;
Group by name, key
select name, key, sum(value) as value
from mytable group by name,key
check this
CREATE TABLE #testing_123
([id] int, [name] varchar(5), [key] int, [value] int)
;
INSERT INTO #testing_123
([id], [name], [key], [value])
VALUES
(1, 'jack', 1, 10),
(2, 'peter', 1, 5),
(3, 'jack', 2, 5),
(4, 'ali', 1, 2),
(5, 'jack', 1, 5),
(6, 'jack', 1, 10),
(7, 'bert', 4, 2),
(8, 'peter', 2, 10),
(9, 'bert', 4, 5)
;
query used was
select min(id) id ,name,[key],sum(value) value from #testing_123 group by name,[key] order by 1
output after insert
For the first part (to get the id column in the way requested), you could work along:
INSERT INTO mynewtable
(id, name, `key`, `value`)
SELECT
MIN(id), name, `key`, SUM(`value`)
FROM mytable
GROUP BY name, `key`
;
Now, provided mynewtable is defined with a unique index on name and key like
CREATE TABLE mynewtable
(id INT, name VARCHAR(5), `key` INT, `value` INT, UNIQUE (name, `key`));
you'd get the requested result with
INSERT INTO mynewtable
(id, name, `key`, `value`)
VALUES
(10, 'jack', 1, 5),
(11, 'bert', 1, 2)
ON DUPLICATE KEY UPDATE `value` = `value` + VALUES(`value`)
;
Beware:
It requires the unique index on name and key to work.
It might not work correctly, if there are other unique indexes and/or a primary key on the same table as well.
NB:
Please try to avoid the use of reserved words such as value and key for, e.g., column names.

Join/merge two tables, improvise/make up "missing" entries

I have two tables, tbl_foo and tbl_bar, and I want to join these tables with tbl_foo.foo_id = tbl_bar.foo_id in the on-clause. However, for each tbl_bar.baz_id there should be one row for each tbl_foo.foo_id (even if no such entry in tbl_bar exists). How do I write such query?
There's more info on the schema and my desired result below.
Edit: Each row must have a foo_id and baz_id.
Edit 2: Added tbl_baz below.
Desired result
+--------+--------+--------+------------+
| bar_id | baz_id | foo_id | some_field |
+--------+--------+--------+------------+
| 1 | 101 | 1 | foo |
| 2 | 101 | 2 | bar |
| 3 | 101 | 3 | baz |
| NULL | 101 | 4 | bin |
| 4 | 102 | 1 | foo |
| NULL | 102 | 2 | bar |
| 5 | 102 | 3 | baz |
| NULL | 102 | 4 | bin |
+--------+--------+--------+------------+
Table: tbl_foo
+--------+------------+
| foo_id | some_field |
+--------+------------+
| 1 | foo |
| 2 | bar |
| 3 | baz |
| 4 | bin |
+--------+------------+
Table: tbl_bar
+--------+--------+--------+
| bar_id | baz_id | foo_id |
+--------+--------+--------+
| 1 | 101 | 1 |
| 2 | 101 | 2 |
| 3 | 101 | 3 |
| 4 | 102 | 1 |
| 5 | 102 | 3 |
+--------+--------+--------+
Table: tbl_baz
+--------+
| baz_id |
+--------+
| 101 |
| 102 |
+--------+
SQL Schema
CREATE TABLE tbl_foo (
foo_id INT,
some_field VARCHAR(255),
PRIMARY KEY (foo_id)
);
INSERT INTO tbl_foo VALUES
(1, 'foo'),
(2, 'bar'),
(3, 'baz'),
(4, 'bin');
CREATE TABLE tbl_bar (
bar_id INT,
baz_id INT,
foo_id INT,
PRIMARY KEY (bar_id, baz_id),
FOREIGN KEY (baz_id) REFERENCES tbl_baz (baz_id),
FOREIGN KEY (foo_id) REFERENCES tbl_foo (foo_id)
);
INSERT INTO tbl_bar VALUES
(1, 101, 1),
(2, 101, 2),
(3, 101, 3),
(4, 102, 1),
(5, 102, 3);
CREATE TABLE tbl_baz (
baz_id INT,
PRIMARY KEY (baz_id)
);
INSERT INTO tbl_baz VALUES
(101),
(102);
Like mwigdalh said, there's no way to achieve that output with the given tables. If there was another baz table, there would be a way. The problem is that the highlighted records below are essentially pulled from thin air, and meaningless. You could just as easily put "meh" in each one, and the output would make as much sense.
+--------+--------+--------+------------+
| bar_id | baz_id | foo_id | some_field |
+--------+--------+--------+------------+
| 1 | 101 | 1 | foo |
| 2 | 101 | 2 | bar |
| 3 | 101 | 3 | baz |
| NULL | *101*| 4 | bin |
| 4 | 102 | 1 | foo |
| NULL | *102*| 2 | bar |
| 5 | 102 | 3 | baz |
| NULL | *102*| 4 | bin |
+--------+--------+--------+------------+
If you provide some context in a closer-to-real-world example, it might be found that there's a different output altogether that achieves your desired result.
You may be looking for a query like this:
UPDATE
Based upon new tbl_baz:
select y.bar_id, x.baz_id, x.foo_id, x.some_field
from (
select a.foo_id, a.some_field, b.baz_id
-- Cross foo_id with all baz_id
from tbl_foo as a, tbl_baz as b
) as x
-- Get the bar_id where it exists for each foo_id/baz_id combo
left join tbl_bar as y on x.foo_id = y.foo_id
and x.baz_id = y.baz_id
order by x.baz_id, x.foo_id
This is based on the assumption that you want to see each foo_id for each baz_id regardless of what is in your many-to-many table.
EXAMPLE of why you may not want this, or may want to update your many-to-many table instead:
If we replace "foo" and "baz" with "person" and "car", this query is essentially saying that every person owns every car. This may be the case, but it is certainly not represented in the "ownership" many-to-many table (bar).
Vague, you can't get there from here. You're asking for a result that specifies a baz_id for rows where there is no corresponding row from tbl_bar. There is simply no way to construct the missing data in this case.
Either your schema is not correct or you need some custom default logic for cases where you can't find a row in tbl_bar.

What does this MySQL statement do?

INSERT IGNORE INTO `PREFIX_tab_lang` (`id_tab`, `id_lang`, `name`)
(SELECT `id_tab`, id_lang, (SELECT tl.`name`
FROM `PREFIX_tab_lang` tl
WHERE tl.`id_lang` = (SELECT c.`value`
FROM `PREFIX_configuration` c
WHERE c.`name` = 'PS_LANG_DEFAULT' LIMIT 1) AND tl.`id_tab`=`PREFIX_tab`.`id_tab`)
FROM `PREFIX_lang` CROSS JOIN `PREFIX_tab`);
It's from an opensource project,and no documentation available.
Especially,what does cross-join mean? I've only used join/left join .
According to the MySQL documentation, it's basically a synonym for INNER JOIN, and INNER JOIN is the same as just JOIN (that is, "INNER" is the default).
Cross-join: http://en.wikipedia.org/wiki/Join_%28SQL%29#Cross_join
The query inserts into PREFIX_tab_lang the results of a select. The select is just two columns from the cross-product. The third column -- name -- actually comes from a totally different select, which is also pretty straight-forward except that one of it's where conditions is yet another select.
In short, this is one of the worst queries I've ever seen. It's preformance is probably horrible, and it should be replaced by a bit of TRANSATION-protected code or, at the very least, a stored procedure.
You can actually consider the following queries to be synonyms in MySQL:
SELECT *
FROM Table1
CROSS JOIN Table2;
SELECT *
FROM Table1, Table2;
SELECT *
FROM Table1
INNER JOIN Table2;
SELECT *
FROM Table1
JOIN Table2;
Test Case:
CREATE TABLE Table1 (id int, value varchar(10));
CREATE TABLE Table2 (id int, t1_id int);
INSERT INTO Table1 VALUES (1, 'Value 1');
INSERT INTO Table1 VALUES (2, 'Value 2');
INSERT INTO Table1 VALUES (3, 'Value 3');
INSERT INTO Table1 VALUES (4, 'Value 4');
INSERT INTO Table2 VALUES (1, 1);
INSERT INTO Table2 VALUES (2, 1);
INSERT INTO Table2 VALUES (3, 2);
INSERT INTO Table2 VALUES (4, 2);
INSERT INTO Table2 VALUES (5, 2);
INSERT INTO Table2 VALUES (6, 3);
INSERT INTO Table2 VALUES (7, 4);
INSERT INTO Table2 VALUES (8, 4);
INSERT INTO Table2 VALUES (9, 4);
All four queries would return the following result set:
+------+---------+------+-------+
| id | value | id | t1_id |
+------+---------+------+-------+
| 1 | Value 1 | 1 | 1 |
| 2 | Value 2 | 1 | 1 |
| 3 | Value 3 | 1 | 1 |
| 4 | Value 4 | 1 | 1 |
| 1 | Value 1 | 2 | 1 |
| 2 | Value 2 | 2 | 1 |
| 3 | Value 3 | 2 | 1 |
| 4 | Value 4 | 2 | 1 |
| 1 | Value 1 | 3 | 2 |
| 2 | Value 2 | 3 | 2 |
| 3 | Value 3 | 3 | 2 |
| 4 | Value 4 | 3 | 2 |
| 1 | Value 1 | 4 | 2 |
| 2 | Value 2 | 4 | 2 |
| 3 | Value 3 | 4 | 2 |
| 4 | Value 4 | 4 | 2 |
| 1 | Value 1 | 5 | 2 |
| 2 | Value 2 | 5 | 2 |
| 3 | Value 3 | 5 | 2 |
| 4 | Value 4 | 5 | 2 |
| 1 | Value 1 | 6 | 3 |
| 2 | Value 2 | 6 | 3 |
| 3 | Value 3 | 6 | 3 |
| 4 | Value 4 | 6 | 3 |
| 1 | Value 1 | 7 | 4 |
| 2 | Value 2 | 7 | 4 |
| 3 | Value 3 | 7 | 4 |
| 4 | Value 4 | 7 | 4 |
| 1 | Value 1 | 8 | 4 |
| 2 | Value 2 | 8 | 4 |
| 3 | Value 3 | 8 | 4 |
| 4 | Value 4 | 8 | 4 |
| 1 | Value 1 | 9 | 4 |
| 2 | Value 2 | 9 | 4 |
| 3 | Value 3 | 9 | 4 |
| 4 | Value 4 | 9 | 4 |
+------+---------+------+-------+
36 rows in set (0.01 sec)