我有以下数据:
select * from art_skills_table;
+----+------+---------------------------+
| ID | Name | skills |
+----+------+---------------------------|
| 1 | Anna | ["painting","photography"]|
| 2 | Bob | ["drawing","sculpting"] |
| 3 | Cat | ["pastel"] |
+----+------+---------------------------+
select * from computer_table;
+------+------+-------------------------+
| ID | Name | skills |
+------+------+-------------------------+
| 1 | Anna | ["word","typing"] |
| 2 | Cat | ["code","editing"] |
| 3 | Bob | ["excel","code"] |
+------+------+-------------------------+
我想编写一条产生下表的SQL语句。
+------+------+-----------------------------------------------+
| ID | Name | skills |
+------+------+-----------------------------------------------+
| 1 | Anna | ["painting","photography","word","typing"] |
| 2 | Bob | ["drawing","sculpting","excel","code"] |
| 3 | Cat | ["pastel","code","editing"] |
+------+------+-----------------------------------------------+
我尝试过类似SELECT * from art_skills_table LEFT JOIN computer_table ON name
的操作。但是,它不能满足我的需求。我已经读过array_cat
了,但是在实现它时遇到了一些麻烦。
答案 0 :(得分:0)
首先,我想知道为什么将数据存储在这样的模型中。 认为NoSQL数据库缺乏联接和...的能力。
有关用例的一些信息会有所帮助。
尽管如此,您可以使用CONCAT
和REPLACE
选择所需格式的数据。
SELECT art_skills_table.ID, computer_table.name,
CONCAT(
REPLACE(art_skills_table.skills, '}',','),
REPLACE(computer_table.skills, '{','')
)
FROM art_skills_table JOIN computer_table ON art_skills_table.ID = computer_table.ID
查询返回以下结果:
+----+------+--------------------------------------------+
| ID | Name | Skills |
+----+------+--------------------------------------------+
| 1 | Anna | {"painting","photography","word","typing"} |
| 2 | Cat | {"drawing","sculpting","code","editing"} |
| 3 | Bob | {"pastel","excel","code"} |
+----+------+--------------------------------------------+
即使Bob的值不同,我也已将ID用于JOIN。 JOIN可能应该在名称上完成。
JOIN computer_table ON art_skills_table.Name = computer_table.Name
顺便说一句,您需要告诉我们您正在运行哪种SQL引擎。