mysql在一个表中连接两个表行

时间:2016-10-21 18:47:20

标签: mysql database

我有两个表e_sku和e_availability,在e_sku表中我有45行,默认可用性为1,下面是一个例子

id  is_available    name
1   1               UN001N
2   1               UN002N
3   1               UN003N
4   1               UN004N
5   1               UN005N
6   1               UN006N
7   1               UN007N
8   1               UN008N
9   1               UN009N
10  1               UN010N
11  1               UN011N
12  1               UN012N
13  1               UN013N
14  1               UN014N
15  1               UN015N
16  1               UN016N
17  1               UN017N
18  1               UN018N
19  1               UN019N
20  1               UN020N
21  1               UN021N
22  1               UN022N
23  1               UN023N
24  1               UN024N
25  1               UN025N
26  1               UN026N
27  1               UN027N
28  1               UN028N
29  1               UN029N
30  1               UN030N
31  1               UN031N
32  1               UN032N
33  1               UN033N
34  1               UN034N
35  1               UN035N
36  1               UN036N
37  1               UN037N
38  1               UN038N
39  1               UN039N
40  1               UN040N
41  1               UN041N
42  1               UN042N
43  1               UN043N
44  1               UN044N
45  1               UN045N

第二个表是这个表中的e_availability我只存储不可用性,下面的日期是示例

id  e_sku_id    is_available    working_date
1   5           0               10/20/2016
2   8           0               10/20/2016
3   10          0               10/20/2016
4   1           0               10/20/2016
5   15          0               10/20/2016
6   11          0               10/19/2016
7   1           0               10/19/2016

因为数据捕获的每个日期都不可用,所以我只在e_availability表w.r.t中收集不可用产品的数据。日期,现在我希望将每天的数据显示为完整的skus,以便每天报告显示45个skus,并且对于可用的sku,它将显示来自e_sku表的列is_available,并且对于不可用的sku,它将显示列来自e_availability表的is_availability

我正在使用mysql数据库,我尝试了很多连接查询但没有收到报告。  可以任何一个指导我需要哪个联接

我使用此查询获得了结果

SELECT * FROM (SELECT id,NAME,1 AS is_available FROM e_sku
WHERE company_id = 2
AND id NOT IN (SELECT id FROM (SELECT e_sku.id,e_sku.name, edge_availability.is_available FROM edge_availability
JOIN edge_working ON edge_working.`id` = edge_availability.`working_id`
JOIN e_sku ON e_sku.id = edge_availability.`sku_id`
WHERE edge_working.`working_date` = '2016-10-19' AND edge_availability.`store_id` = 84) X)
UNION
SELECT e_sku.id,e_sku.name, edge_availability.is_available FROM edge_availability
JOIN edge_working ON edge_working.`id` = edge_availability.`working_id`
JOIN e_sku ON e_sku.id = edge_availability.`sku_id`
WHERE edge_working.`working_date` = '2016-10-19' AND edge_availability.`store_id` = 84) Y
ORDER BY id

1 个答案:

答案 0 :(得分:0)

您可以使用LEFT JOIN检测何时在e_availability表中找不到日期。对于该表中的所有列,联接将返回NULL,然后您可以默认使用e_sku中的值。

SELECT d.date, s.id, IFNULL(a.is_available, s.is_available) AS is_available
FROM all_dates AS d
CROSS JOIN e_sku AS s
LEFT JOIN e_availability ON d.working_date = a.date AND s.id = a.e_sku_id

您需要创建一个附加表all_dates,其中包含您要报告的所有日期。有关如何动态创建此类表的信息,请参阅What is the most straightforward way to pad empty dates in sql results (on either mysql or perl end)?