在两个表mysql上左连接时获取最后修改日期
Posted
技术标签:
【中文标题】在两个表mysql上左连接时获取最后修改日期【英文标题】:Get last modified date when left join on two tables mysql 【发布时间】:2021-09-08 15:26:15 【问题描述】:我有主表 advertiser 并希望在 ads_id 上左连接两个表 LayoutVariables 和 LayoutMapping 并在这两个表中获取最后修改日期最终结果中的表格列。
下面的查询只给出一条记录,我想要 advertiser 表中的所有值,即使这两个表 LayoutVariables 和 LayoutMapping
SELECT
ADV.av_id,
ADV.av_advertiser_name,
ADV.av_is_active,
IF(AVLV.alv_advertiser_id IS NOT NULL, TRUE, FALSE) AS isBrandConfigDone,
IF(AVLM.alm_advertiser_id IS NOT NULL, TRUE, FALSE) AS isLayoutMappingDone,
---here i want the max date between two tables
IF(AVLM.alm_last_modified_on IS NOT NULL AND AVLV.alv_last_modified_on IS NOT NULL,IF(MAX(AVLM.alm_last_modified_on)>MAX(AVLV.alv_last_modified_on), "AVLM", "AVLV"),NULL) AS mm
FROM DB.Advertiser AS ADV
LEFT JOIN DB.LayoutVariables AS AVLV ON ADV.av_id = AVLV.alv_advertiser_id
LEFT JOIN DB.LayoutMapping AS AVLM ON ADV.av_id = AVLM.alm_advertiser_id
WHERE ADV.av_is_active=1;
表格布局变量
alv_id | alv_advertiser_id | alv_layout_variable_key | alv_layout_variable_value | alv_last_modified_on | alv_last_modified_by |
---|---|---|---|---|---|
11 | 8167 | var4 | var val 40 | 1624550014 | 2025 |
12 | 7690 | amazon_usa | amazon_usa_vairaible | 1624589928 | 2021 |
表格布局映射
alm_id | alm_advertiser_id | alm_layout_id | alm_last_modified_on | alm_last_modified_by |
---|---|---|---|---|
5 | 8167 | 334455 | 1624436972 | 2025 |
12 | 7690 | 123 | 1624320000 | 2021 |
餐桌广告商
av_id | av_advertiser_name | av_is_active |
---|---|---|
8167 | TestAdvertiser | 1 |
7690 | amazon_usa | 1 |
CREATE TABLE LayoutVariable (
alv_id INT,alv_advertiser_id INT,alv_layout_variable_key VARCHAR(200),
alv_layout_variable_value VARCHAR(200),alv_last_modified_on INT,alv_last_modified_by INT);
INSERT INTO LayoutVariable VALUES
(11,8167,'var4','var val 40',1624550014,2025),
(12,7690,'amazon_usa','amazon_usa_vairaible',1624589928,2021);
CREATE TABLE LayOutmapping (
alm_id INT,alm_advertiser_id INT,alm_layout_id INT,
alm_last_modified_on INT,alm_last_modified_by INT);
INSERT INTO LayOutmapping VALUES
(5 ,8167 ,334455 ,1624436972 ,2025)
(12 ,7690 ,123 ,1624320000 ,2021);
CREATE TABLE Advertiser (
av_id INT,av_advertiser_name VARCHAR(200),av_is_active INT);
INSERT INTO Advertiser VALUES
(8167,'TestAdvertiser',1),
(7690,'amazon_usa',1);
有人可以在这里指导我吗
【问题讨论】:
在输出列表中使用相关子查询。 你能举一些例子来满足上述要求吗 为相关表格提供样本数据并显示所需结果。 @FaNo_FN 提供,请看 【参考方案1】:你可以这样做:
SELECT
ADV.av_id,
ADV.av_advertiser_name,
ADV.av_is_active,
AVLV.alv_advertiser_id ,
AVLM.alm_advertiser_id ,
IF(AVLV.alv_advertiser_id IS NOT NULL, TRUE, FALSE) AS isBrandConfigDone,
IF(AVLM.alm_advertiser_id IS NOT NULL, TRUE, FALSE) AS isLayoutMappingDone,
(SELECT MAX(lmd) FROM
(SELECT alm_last_modified_on lmd FROM LayoutMapping UNION
SELECT alv_last_modified_on FROM LayoutVariables) v) AS mm
FROM Advertiser AS ADV
LEFT JOIN LayoutVariables AS AVLV ON ADV.av_id = AVLV.alv_advertiser_id
LEFT JOIN LayoutMapping AS AVLM ON ADV.av_id = AVLM.alm_advertiser_id
;
或在SELECT
列表中使用相同的子查询,将其设为CROSS JOIN
,例如:
SELECT
ADV.av_id,
ADV.av_advertiser_name,
ADV.av_is_active,
AVLV.alv_advertiser_id ,
AVLM.alm_advertiser_id ,
IF(AVLV.alv_advertiser_id IS NOT NULL, TRUE, FALSE) AS isBrandConfigDone,
IF(AVLM.alm_advertiser_id IS NOT NULL, TRUE, FALSE) AS isLayoutMappingDone,
mm
FROM Advertiser AS ADV
LEFT JOIN LayoutVariables AS AVLV ON ADV.av_id = AVLV.alv_advertiser_id
LEFT JOIN LayoutMapping AS AVLM ON ADV.av_id = AVLM.alm_advertiser_id
CROSS JOIN (SELECT MAX(lmd) AS mm FROM
(SELECT alm_last_modified_on lmd FROM LayoutMapping UNION
SELECT alv_last_modified_on FROM LayoutVariables) v) mx
您最初尝试在整个查询中使用MAX()
,而上面的这些方法是与查询的其余部分分开的子查询。
Demo fiddle
【讨论】:
非常感谢您的回答,我已经运行了上面的查询,它为所有行提供 mm,这个值 1624589928 也适用于其他行 不,只匹配行 mm shd 来,不匹配它 shd 来 null 你的意思是这样? dbfiddle.uk/… 是的,但是对于 8167,LayoutVariables 表中的这个值 1624550014 大于 LayoutMapping 中的值,并且在您的查询中,它将为 null,如果 av_id 没有值,则 null shd 出现 好的,那这个怎么样? dbfiddle.uk/…以上是关于在两个表mysql上左连接时获取最后修改日期的主要内容,如果未能解决你的问题,请参考以下文章