Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.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
Mysql 如何将左表中的所有行与右表相乘_Mysql_Sql - Fatal编程技术网

Mysql 如何将左表中的所有行与右表相乘

Mysql 如何将左表中的所有行与右表相乘,mysql,sql,Mysql,Sql,我有以下两个表格: Table: ProductList Product; Product a Procuct b Product c ... Product Z Table: SalesTable Agent, Product, Qty ZXY, Product A, 200 ABC, Product A, 100 ABC, Product B, 200 我想要一张有所有产品清单*代理商清单的表格。 因此,对于Instance: Agen

我有以下两个表格:

  Table: ProductList
  Product;
   Product a
   Procuct b 
   Product c
   ...
   Product Z

Table: SalesTable
Agent, Product, Qty
 ZXY, Product A, 200
 ABC, Product A, 100
 ABC, Product B, 200
我想要一张有所有产品清单*代理商清单的表格。 因此,对于Instance:

   Agent, Product, Qty
   ZXY, Product A, 200
   ZXY, Product B,  -
   ZXY, Product C,  - 
   ....
   ABC, Product A, 100
   ABC, Product B, 200
   ABC, Product C, -
   ABC, Product D, - 
使用:

SELECT * 
  FROM ProductTable
       LEFT JOIN AgentTable 
            ON ProductTable.Product = SalesTable.Product
显然不管用

试试这个查询

SELECT ST.*
FROM ProductList PL
JOIN SalesTable ST ON PL.Product = ST.Product
ORDER BY ST.Agent, ST.Product
删除了分组依据,因为需要顺序,而不是分组

SQLFIDLE-

您可以这样尝试

Select Distinct B.Agent,A.Product,C.Qty

from

Product A

Cross JOIN SalesTable B 

Left join SalesTable C on C.Product=A.Product and C.Agent=B.AGENT

SELECT t1.Agent, t1.Product, COALESCE(s.Qty, '-') AS Qty
  FROM (
        SELECT a.Agent, p.Product
          FROM (SELECT DISTINCT Agent FROM SalesTable) a
               CROSS JOIN
               (SELECT DISTINCT Product FROM ProductList) p
        ) t1 LEFT JOIN SalesTable s
                  ON (t1.Product = s.Product 
                      AND t1.Agent = s.Agent)