我正在尝试为电子邮件线程创建dbase解决方案。当我收到电子邮件时..我想知道这封电子邮件是否属于现有的帖子。
所以我匹配主题即subject ='cool bro'匹配're:cool bro'
我还想匹配发送者和接收者对
即
(sender = 'A@gmail.com' and receiver = 'B@gmail.com')
OR
(sender ='B@gmail.com' and receiver = 'A@gmail.com')
对于那些确切的情况..此查询工作正常(请参阅更多详细信息here):
(SELECT COUNT(search_email.threadID) FROM search_email
WHERE search_email.subject MATCH '%query%' AND
(
(search_email.sender = '%sender' AND search_email.tos = '%receiver%')
OR
(search_email.sender = '%receiver%' AND search_email.tos = '%sender%')
)
问题是当你遇到这样的情况时:
(sender = 'Amanda Collins A@gmail.com' and receiver = 'B@gmail.com')
OR
(sender ='Billy Bob B@gmail.com' and receiver = 'A@gmail.com')
很明显..这里有一个MATCH子句(或者一些正则表达式或者somthing)。
问题是sqllite doesn't allow MATCH
与OR
合作:需要UNION
而不是..
但我无法找到将上述语句转换为UNION/INTERSECT
的语句的方法:
SELECT * FROM search_email WHERE
search_email.subject MATCH '%query%' INTERSECT SELECT * FROM
(SELECT * FROM (SELECT * FROM search_email WHERE
search_email.sender MATCH '%sender%'
INTERSECT SELECT * FROM search_email WHERE search_email.tos MATCH '%receiver%' )
UNION
SELECT * FROM( SELECT * FROM search_email WHERE search_email.sender MATCH '%sender%'
INTERSECT SELECT * FROM search_email WHERE search_email.tos MATCH '%receiver%'))
任何想法?
更新的
似乎答案只是将第一个intersect
更改为union
:
SELECT * FROM search_email WHERE
search_email.subject MATCH '%query%' UNION SELECT * FROM
(SELECT * FROM (SELECT * FROM search_email WHERE
search_email.sender MATCH '%sender%'
INTERSECT SELECT * FROM search_email WHERE search_email.tos MATCH '%receiver%' )
UNION
SELECT * FROM( SELECT * FROM search_email WHERE search_email.sender MATCH '%sender%'
INTERSECT SELECT * FROM search_email WHERE search_email.tos MATCH '%receiver%'))
但是这个解决方案似乎非常效率低下.. mysql编译器需要一段时间来弄清楚..是否有更有效的方法?
答案 0 :(得分:0)
使用MATCH
或LIKE '%string%'
将始终非常缓慢。您可以使用全文搜索来改进它,但您仍然不会走得太远。
还有另一种方法可以做到这一点,它更有效率并得到广泛支持。
根据RFC2822(和较早的RFC822)邮件客户端程序应该在邮件头中创建和维护字段,以便于线程支持。实际上,所有已知的邮件客户端实际上都支持它。那些字段是:
<randomstring@sender.com>
如果您提取这些标题并将它们保存在数据库中的单独的列和/或表中,您应该能够通过使用精确比较(无MATCH '%string%
')轻松创建线程,因此它将非常快