Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.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 - Fatal编程技术网

Mysql 跨主查询和子查询的表联接

Mysql 跨主查询和子查询的表联接,mysql,Mysql,我的数据库包含以下表: TABLE SALESCOUNT ----------------------------------- | id | startWeek | repID | sales | ----------------------------------- | 1 | FY13M01W01 | 0001 | 4 | | 2 | FY13M01W01 | 0002 | 8 | | 3 | FY13M01W02 | 0001 | 5 | | 4

我的数据库包含以下表:

TABLE SALESCOUNT
-----------------------------------
| id | startWeek  | repID | sales |
-----------------------------------
| 1  | FY13M01W01 | 0001  |   4   |
| 2  | FY13M01W01 | 0002  |   8   |
| 3  | FY13M01W02 | 0001  |   5   |
| 4  | FY13M01W02 | 0002  |   9   |
...

TABLE SALES
----------------------------------------------------
| id | salesRepID | salesDate  | division  | team  |
----------------------------------------------------
| 1  |   0001     | 2013-01-01 |   divA    | teamA |
| 2  |   0002     | 2013-01-01 |   divC    | teamA |
| 3  |   0001     | 2013-01-01 |   divD    | teamD |
| 4  |   0002     | 2013-01-08 |   divB    | teamC |
...

TABLE DEPT
-------------------------------
| id | repID | region | dept  |
-------------------------------
| 1  | 0001  |   01   |   A   |
| 2  | 0002  |   02   |   B   |
| 3  | 0001  |   C    |   A   |
| 4  | 0002  |   02   |   B   |
...

TABLE GEO
-----------------------------------
| id | geo   | regionA | regionB  |
-----------------------------------
| 1  | geoA  |   01    |     A    |
| 2  | geoA  |   02    |     B    |
| 3  | geoB  |   03    |     C    |
| 4  | geoB  |   04    |     D    |
...

TABLE FISCALWEEK
--------------------------------
| id | salesDate  | startWeek  |
--------------------------------
| 1  | 2013-01-01 | FY13M01W01 |
| 2  | 2013-01-08 | FY13M01W02 |
...
以下查询返回所需的结果:

SELECT y.startWeek, SUM(y.sales), dept.dept, geo.geo
FROM salesCount as y,
(
    SELECT DISTINCT a.salesRepID
    FROM sales AS a
    WHERE (a.team IN ("teamA", "teamB", "teamC)
        AND a.saleDate BETWEEN "2013-01-01" AND "2013-06-30"
        AND a.division IN ("divA", "divB", "divC")
) as x
INNER JOIN dept
    ON x.salesRepID = dept.repID
INNER JOIN geo
    ON dept.region = geo.regionA OR dept.region = geo.regionB
WHERE x.salesRepID = y.repID
    AND y.startWeek BETWEEN "FY13M01W01" AND "FY13M06W04"
GROUP BY y.startWeek, geo.geo, dept.dept
ORDER BY y.startWeek ASC, geo.geo ASC, dept.dept ASC
我的目标是利用FISCALWEEK表生成2个日期对(1个在子查询的WHERE子句中,1个在主查询的WHERE子句中)。我希望能够在单个位置输入日期对(xxxx xx xx),并使用它以所需格式在两个区域填充日期。不幸的是,我无法更改数据或表格


我已经寻找了很长一段时间,似乎找不到一个我能够适应我的情况的答案。我已经尝试了许多排列,这是唯一一个我能想出的,给我想要的结果。如果您有任何问题或需要任何其他信息,请告诉我。非常感谢您的帮助。谢谢。

这是我想不起来的,也许你可以先将saleDate提取到外部查询中:

SELECT y.startWeek, SUM(y.sales), dept.dept, geo.geo
FROM salesCount as y,
(
    SELECT DISTINCT a.salesRepID, a.saleDate
    FROM sales AS a
    WHERE (a.team IN ("teamA", "teamB", "teamC)
        AND a.division IN ("divA", "divB", "divC")
) as x
INNER JOIN dept
    ON x.salesRepID = dept.repID
INNER JOIN geo
    ON dept.region = geo.regionA OR dept.region = geo.regionB
WHERE x.salesRepID = y.repID
    AND y.startWeek BETWEEN "FY13M01W01" AND "FY13M06W04"
    AND x.saleDate BETWEEN "2013-01-01" AND "2013-06-30"
GROUP BY y.startWeek, geo.geo, dept.dept
ORDER BY y.startWeek ASC, geo.geo ASC, dept.dept ASC
然后,您可以从中查看加入FISCALWEEK表的情况,例如:

SELECT y.startWeek, SUM(y.sales), dept.dept, geo.geo
FROM salesCount as y,
(
    SELECT DISTINCT a.salesRepID, a.saleDate
    FROM sales AS a
    WHERE (a.team IN ("teamA", "teamB", "teamC)
        AND a.division IN ("divA", "divB", "divC")
) as x
INNER JOIN dept
    ON x.salesRepID = dept.repID
INNER JOIN geo
    ON dept.region = geo.regionA OR dept.region = geo.regionB
INNER JOIN fiscalweek
    ON y.startWeek = fiscalweek.startWeek
WHERE x.salesRepID = y.repID
    AND y.startWeek BETWEEN "FY13M01W01" AND "FY13M06W04"
    AND x.saleDate = fiscalweek.salesDate 
GROUP BY y.startWeek, geo.geo, dept.dept
ORDER BY y.startWeek ASC, geo.geo ASC, dept.dept ASC

不确定这是否可行,但希望能给您一些想法

那么,您是否正在寻找同时更新
SALES.salesDate
FISCALWEEK.salesDate
的更新查询?您可以创建一个子查询,其中只包含一个具有所需日期的注册表,并使用它加入查询。类似于
(选择日期格式('2013-01-08','%Y-%m-%d')作为dtvalue