简体   繁体   English

访问或 Mysql - 表行作为查询中的列

[英]Access or Mysql - Table rows as columns in query

I have these three tables:我有这三个表:

在此处输入图像描述

The table "Clienti" contains the customers.表“Clienti”包含客户。 The table "Corsi" contains all the available courses The table "corsi Fatti" contains all the Courses each client has taken. “Corsi”表包含所有可用的课程 “corsi Fatti”表包含每个客户参加的所有课程。

what I would need is a query that returns each client, and what courses he attended on what date.我需要的是一个返回每个客户的查询,以及他在什么日期参加了哪些课程。 For that I would like to have for example a table returned with these columns:为此,我希望例如返回一个包含这些列的表:

Clienti.Nome, corsi.row1.corso, corsi.row2.corso, corsi.row3.corso,corsi.rowN.corso. Clienti.Nome、corsi.row1.corso、corsi.row2.corso、corsi.row3.corso、corsi.rowN.corso。

and the content of the table should be: clienti.Nome, corsifatti.data of the matching course in the corsi table if present.并且表的内容应该是:clienti.Nome,corsi 表中匹配课程的 corsifatti.data(如果存在)。

so, first column is the client name, and then there is a column for each row of the "corsi" table, and if a client has partecipated on that course then the corsifatti.data should be in that column.因此,第一列是客户名称,然后“corsi”表的每一行都有一列,如果客户参与了该课程,那么 corsifatti.data 应该在该列中。

Can something like this be done with a Access or Mysql Query?可以使用 Access 或 Mysql 查询来完成类似的操作吗? I have tried with inner joins but the result was not what I need.我尝试过内部连接,但结果不是我需要的。

select
  Clienti.nome, Clienti.Addresse, Clienti.CAP, Clienti.Tel,
  Clienti.Ansprechpartner, Clienti.Mail, Clienti.Weiteres,
  CorsiFatti.Data, Corsi.Corso, Corsi.Durata 
from Clienti 
INNER JOIN CorsiFatti on CorsiFatti.[ID Cliente] = Clienti.ID 
INNER JOIN Corsi on Corsi.ID = CorsiFatti.[ID Corso]

What you are asking is a simple inner join:您要问的是一个简单的内部联接:

select Clienti.nome, Clienti.Addresse, Clienti.CAP, 
   Clienti.Tel, Clienti.Ansprechpartner, Clienti.Mail, 
   Clienti.Weiteres, 
   CorsiFatti.Data, 
   Corsi.Corso, Corsi.Durata 
from Clienti 
INNER JOIN CorsiFatti on CorsiFatti.[ID Cliente] = Clienti.ID 
INNER JOIN Corsi on Corsi.ID = CorsiFatti.[ID Corso]
order by Clienti.nome, Corsi.Corso;

I think you meant yours was lacking the order by only.我想你的意思是你的只是缺少订单。 I wouldn't suggest using access, but if it is access anyway, then you need to have parenthseses around all those joins (peculiar I know, but it is access).我不建议使用访问,但如果它仍然是访问,那么你需要在所有这些连接周围加上括号(我知道很奇怪,但它是访问)。

The pivot with variable columns count is complicated.具有可变列数的 pivot 很复杂。 I can advice simplest solution uses JSON aggregation:我可以建议最简单的解决方案使用 JSON 聚合:

SELECT 
  ClienteId, 
  Name, 
  JSON_ARRAYAGG(
    JSON_OBJECT("Data", Data, "Corso", Corso)
  ) Corsi 
FROM 
  CorsiFatti 
  JOIN Corsi ON Corsi.Id = CorsiFatti.CorsoId 
  JOIN Clienti ON Clienti.Id = CorsiFatti.clienteId 
GROUP BY 
  ClienteId, 
  Name
;

Result is row for each client contains client's data at his courses as JSON string:结果是每个客户的行包含客户在其课程中的数据,如 JSON 字符串:

+===========+=========+========================================================================================+
| ClienteId | Name    | Corsi                                                                                  |
+===========+=========+========================================================================================+
| 1         | Mr. Fix | [{"Data": "2021-01-01", "Corso": "Corso1"}, {"Data": "2021-02-01", "Corso": "Corso3"}] |
+-----------+---------+----------------------------------------------------------------------------------------+

Here you can find SQL fiddle在这里你可以找到SQL 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM