Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 是否有一种方法可以在分组方式的一个结果中进行一对多+一对多查询?_Sql_Mysql_Select - Fatal编程技术网

Sql 是否有一种方法可以在分组方式的一个结果中进行一对多+一对多查询?

Sql 是否有一种方法可以在分组方式的一个结果中进行一对多+一对多查询?,sql,mysql,select,Sql,Mysql,Select,我知道标题让人困惑。下面我举一个例子。我知道如何使用内联选择,请尽量避免: T1 id | title 1 a T2 t1_id | title 1 a1 1 a2 1 a3 T3 t1_id | amount 1 10 The result set should be: t1.id, group_concat(t2.title) , sum(t3.amount)

我知道标题让人困惑。下面我举一个例子。我知道如何使用内联选择,请尽量避免:

T1
id | title
1     a


T2
t1_id | title
1        a1
1        a2
1        a3


T3
t1_id | amount
1         10


The result set should be: t1.id, group_concat(t2.title) , sum(t3.amount)
                            1   | a1,a2,a3 |                   10
这个怎么样

select t2.t1_id, group_concat(t2.title), t3.amount
from t2
join t3 on t2.t1_id = t3.t1_id
group by t2.t1_id, t3.amount

为了澄清,内联选择包括像SELECT T1.*,从t2中选择group_concatt2.title,其中t2.T1_id=T1.id?@ajreal-是的,这是正确的。作为参考,内联选择通常被称为子查询。这将返回30,而不是10。哦,他说了SUM,所以我认为这是他想要的。我已经编辑了可能有效的SQL。OP想要求和,但他们不想将同一行作为另一个表上联接的副产品进行多次计数…所以t3没有规范化?也就是说,单个t1_id是否可以有多个不同金额的条目/记录?@suhprano它是100%标准化的。因为t2和t2的连接将产生3个条目,其中一个t3副本将被附加,总数将给出30个条目,而不是我需要的10个条目。