简体   繁体   English

MySQL并且不能按预期工作

[英]MySQL AND not working as expected

First of all, here is the query I am trying to use. 首先,这是我要使用的查询。

SELECT * FROM Messages t WHERE perspective_user=? and timestamp BETWEEN ? AND ? AND 
timestamp_added = 
(select max(timestamp_added) from Messages t2 
where 
t2.author = t.author AND
t2.body = t.body AND
t2.timestamp = t.timestamp AND
t2.timestamp_added <= ?
) AND convo_id IN (SELECT convo_id FROM Conversations WHERE perspective_user=? AND identity=? AND timestamp_added=?);

The code works fine without the following line, but with it, it returns 0 results. 该代码在没有以下行的情况下可以正常工作,但是有了它,它返回0个结果。

AND convo_id IN (SELECT convo_id FROM Conversations WHERE perspective_user=? AND identity=? AND timestamp_added=?);

I've also verified that the above line works on its own, tested on a basic SELECT * query. 我还验证了上面的行是独立运行的,并在基本的SELECT *查询上进行了测试。 I've also made sure that there are rows in the database that satisfy the top query. 我还确保数据库中有满足最高级查询的行。

The data is a log of Skype messages. 数据是Skype消息的日志。 Logs are taken randomly; 日志是随机抽取的; the goal is to get the most recent log that is still in the database. 目标是获取仍在数据库中的最新日志。 The goal is to be able to graph my Skype messages and do other fun things. 目的是能够绘制我的Skype消息并做其他有趣的事情。 Skype purges its database on my computer of older messages. Skype会在我的计算机上清除较旧消息的数据库。 The long "timestamp_added" statement is to get the newest of each message, so that if the latest log had a purged database, old messages would still be included. 长的“ timestamp_added”语句用于获取每条消息的最新消息,因此,如果最新日志具有已清除的数据库,则仍将包含旧消息。

First thing, that I see is that you have a space between IN and parenthesis — MySQL "hates" it. 我首先看到的是,IN和括号之间有一个空格-MySQL“讨厌”它。 Fix it like that: 像这样修复它:

...AND convo_id IN(SELECT... 

But if it's just topic posting typo error, then please share your data (MySQL scheme of Messages and Conversations ). 但是,如果仅是发布拼写错误的主题,那么请共享您的数据(MySQL的Messages and Conversations方案)。

The statement below return a list of rows where as what you need to pass to an IN() function is a CSV. 下面的语句返回一个行列表,其中需要传递给IN()函数的是CSV。

(SELECT convo_id FROM Conversations WHERE perspective_user=? AND identity=? AND timestamp_added=?)

So instead use GROUP_CONCAT() like this 因此,改为使用GROUP_CONCAT()

(SELECT GROUP_CONCAT(convo_id) FROM Conversations WHERE perspective_user=? AND identity=? AND timestamp_added=? GROUP BY convo_id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM