MySQL LEFT Join with multiple possibilities 和 Inner Join
MySQL LEFT Join with multiple possibilities and Inner Join
我不知道是否有人问过类似的问题,但我在 Whosebugmysql 上看了一个多小时
我的问题是,我有多个 table,我需要在 mysql
中使用左连接和内连接来连接它们
实体table:
id (key) | entityName
1 | john
2 | harris
3 | henry
4 | mark
5 | dom
Activity table
id (key) | entityID | status
1 | 1 | 1
2 | 2 | 0
3 | 4 | 1
地理数据table
id (key) | entityID | moment (timestamps when the entry was done)
1 | 1 | 1429542320 (smaller)
2 | 1 | 1429542331 (bigger)
3 | 2 | 1429542320 (smaller)
4 | 2 | 1429542331 (biger)
5 | 4 | 1429542331 (bigger)
信息table
id (key) | entityID | infos | date
1 | 1 | xxx | today
2 | 1 | xxx | yesterday
3 | 2 | xxx | today
4 | 2 | xxx | yesterday
5 | 3 | xxx | yesterday
6 | 5 | xxx | today
7 | 5 | xxx | yesterday
8 | 5 | xxx | tomorrow
所以基本上,我需要每个有今天信息的实体
此外,如果他们的状态为真(或 1)(来自 activity table),请在地理数据 table.
中显示他们的日期
这就是我得到的:
SELECT e.id,
e.entityName,
i.infos,
a.status,
MAX(g.moment) -- but the max only if status =1
FROM entities AS e
LEFT JOIN activity AS a ON a.entityID = e.id
LEFT JOIN geodata AS g ON g.entityID = e.id
INNER JOIN infos AS i ON e.id = i.entityID
WHERE i.date = 'today'
GROUP BY e.id
我想要每个实体都有关于今天的信息,但其中一些实体也有 activity,所以我想展示它(如果它不只是让左连接放 NULL)如果status 为 0,我不需要 moment,但如果它是 true,我只需要更大的那个(它的数字,所以 Max() 应该这样做但它坏了)
预期结果是:
id (key) | entityName | infos | status | MAX(moment) | ..other columns
1 | john | xxx | 1 | 1429542331 (the bigger one)
2 | harris | xxx | 0 | NULL
5 | dom | xxx | NULL | NULL
如果有人能帮助我,我将不胜感激:)
PS.: 对不起我的英语,它不是我的第一语言
您可以更改
MAX(g.moment)
到
IF(a.status<>1, NULL, MAX(g.moment))
或者将 LEFT JOIN geodata AS g ON g.entityID = e.id
更改为 LEFT JOIN geodata AS g ON a.entityID = e.id AND a.status = 1
哪个更快可能取决于你的实际数据;第二个可能更快,因为连接的记录较少,但它使用的更复杂的连接条件可能会减慢连接速度。
我不知道是否有人问过类似的问题,但我在 Whosebugmysql 上看了一个多小时
我的问题是,我有多个 table,我需要在 mysql
中使用左连接和内连接来连接它们实体table:
id (key) | entityName
1 | john
2 | harris
3 | henry
4 | mark
5 | dom
Activity table
id (key) | entityID | status
1 | 1 | 1
2 | 2 | 0
3 | 4 | 1
地理数据table
id (key) | entityID | moment (timestamps when the entry was done)
1 | 1 | 1429542320 (smaller)
2 | 1 | 1429542331 (bigger)
3 | 2 | 1429542320 (smaller)
4 | 2 | 1429542331 (biger)
5 | 4 | 1429542331 (bigger)
信息table
id (key) | entityID | infos | date
1 | 1 | xxx | today
2 | 1 | xxx | yesterday
3 | 2 | xxx | today
4 | 2 | xxx | yesterday
5 | 3 | xxx | yesterday
6 | 5 | xxx | today
7 | 5 | xxx | yesterday
8 | 5 | xxx | tomorrow
所以基本上,我需要每个有今天信息的实体 此外,如果他们的状态为真(或 1)(来自 activity table),请在地理数据 table.
中显示他们的日期这就是我得到的:
SELECT e.id,
e.entityName,
i.infos,
a.status,
MAX(g.moment) -- but the max only if status =1
FROM entities AS e
LEFT JOIN activity AS a ON a.entityID = e.id
LEFT JOIN geodata AS g ON g.entityID = e.id
INNER JOIN infos AS i ON e.id = i.entityID
WHERE i.date = 'today'
GROUP BY e.id
我想要每个实体都有关于今天的信息,但其中一些实体也有 activity,所以我想展示它(如果它不只是让左连接放 NULL)如果status 为 0,我不需要 moment,但如果它是 true,我只需要更大的那个(它的数字,所以 Max() 应该这样做但它坏了)
预期结果是:
id (key) | entityName | infos | status | MAX(moment) | ..other columns
1 | john | xxx | 1 | 1429542331 (the bigger one)
2 | harris | xxx | 0 | NULL
5 | dom | xxx | NULL | NULL
如果有人能帮助我,我将不胜感激:) PS.: 对不起我的英语,它不是我的第一语言
您可以更改
MAX(g.moment)
到
IF(a.status<>1, NULL, MAX(g.moment))
或者将 LEFT JOIN geodata AS g ON g.entityID = e.id
更改为 LEFT JOIN geodata AS g ON a.entityID = e.id AND a.status = 1
哪个更快可能取决于你的实际数据;第二个可能更快,因为连接的记录较少,但它使用的更复杂的连接条件可能会减慢连接速度。