我有一个返回5列的查询。
其中一列显示行的最新日期值,其中一列包含字符串“seller-contact”或“buyer-contact”。
原始代码工作正常,看起来像这样。
descriptionLabel
我尝试使用两个子查询来实现上述结果。它不会失败或导致任何错误代码。但是在较大的LIVE数据库上,它花费的时间太长了(返回大约270行,我假设使用子查询会产生270 ^ 270x之前的时间;它永远不会完成它只是挂起)
if ([json valueForKey:@"unavailable"]) {
UnavailableViewController* controller = [[UnavailableViewController alloc] init];
controller.descriptionLabel.text = ([json valueForKey:@"unavailable_text"]) ? [json valueForKey:@"unavailable_text"] : @"We're temporarily unavailable, please try again later."; // just in case you forget to set it have a fallback.
// present view controller...
}
有没有办法让事情更简单,所以我可以有一些看起来像
的东西SELECT
TRIM(p.title) AS `Property Address`,
TRIM(us.name) AS `Staff`,
DATE_FORMAT(FROM_UNIXTIME(MAX( n.datecreated)), '%d-%m-%Y') AS `Last Contact Note`,
DATEDIFF(
DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP(NOW())), '%Y-%m-%d'),
DATE_FORMAT(FROM_UNIXTIME(MAX(n.datecreated)), '%Y-%m-%d')
) AS `Days Since Last Note`
FROM ias_property AS `p`
LEFT JOIN ias_note_stream AS `ns` ON ns.target_id = p.id
LEFT JOIN ias_note AS `n` ON ns.note_id = n.id
LEFT JOIN ias_user_staff AS `us` ON p.user_staff_id = us.id
WHERE p.status = 'sold' AND p.isarchived = 0
AND us.position = 'SP'
AND (n.data LIKE '%buyer-contact%' OR n.data LIKE '%seller-contact%')
GROUP BY p.id
ORDER BY MAX(n.datecreated) ASC
TLDR:如何将同一列中的WHERE子句的结果显示为两个单独的列(AS SELECT
TRIM(p.title) AS `Property Address`,
TRIM(us.name) AS `Staff`,
(SELECT DATE_FORMAT(FROM_UNIXTIME(MAX(n.datecreated)), '%d-%m-%Y')
FROM ias_property AS `p`
LEFT JOIN ias_note_stream AS `ns` ON ns.target_id = p.id
LEFT JOIN ias_note AS `n` ON ns.note_id = n.id
WHERE n.data LIKE '%buyer-contact%'
AND p.status = 'sold' AND p.isarchived = 0
AND us.position = 'SP'
) AS `Last Buyer Contact Note`,
(SELECT DATE_FORMAT(FROM_UNIXTIME(MAX(n.datecreated)), '%d-%m-%Y')
FROM ias_property AS `p`
LEFT JOIN ias_note_stream AS `ns` ON ns.target_id = p.id
LEFT JOIN ias_note AS `n` ON ns.note_id = n.id
WHERE n.data LIKE '%seller-contact%'
AND p.status = 'sold' AND p.isarchived = 0
AND us.position = 'SP'
) AS `Last Seller Contact Note`,
DATEDIFF(
DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP(NOW())), '%Y-%m-%d'),
DATE_FORMAT(FROM_UNIXTIME(MAX(n.datecreated)), '%Y-%m-%d')
) AS `Days Since Last Note`
FROM ias_property AS `p`
LEFT JOIN ias_note_stream AS `ns` ON ns.target_id = p.id
LEFT JOIN ias_note AS `n` ON ns.note_id = n.id
LEFT JOIN ias_user_staff AS `us` ON p.user_staff_id = us.id
WHERE p.status = 'sold' AND p.isarchived = 0
AND us.position = 'SP'
AND (n.data LIKE '%buyer-contact%' OR n.data LIKE '%seller-contact%')
GROUP BY p.id
ORDER BY MAX(n.datecreated) ASC
,AS DATE_FORMAT(FROM_UNIXTIME(MAX(
n.datecreated WHERE n.data LIKE '%seller-%'
)), '%d-%m-%Y') AS `Last Buyer Contact Note;
)等?
答案 0 :(得分:1)
您可以在最大函数中放置IF
以获取特定值的最大值。
DATE_FORMAT(FROM_UNIXTIME(MAX(IF(n.data LIKE '%buyer-contact%',n.datecreated,NULL))), '%d-%m-%Y') AS `Last Buyer Contact Note`
DATE_FORMAT(FROM_UNIXTIME(MAX(IF(n.data LIKE '%seller-contact%',n.datecreated,NULL))), '%d-%m-%Y') AS `Last Seller Contact Note`
添加这些不应该影响原始查询的性能。