Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL查询显示未查看或最近10天内的记录_Mysql_Sql_Select - Fatal编程技术网

MySQL查询显示未查看或最近10天内的记录

MySQL查询显示未查看或最近10天内的记录,mysql,sql,select,Mysql,Sql,Select,我有3个表(客户、消息和消息线程)。我想执行一个查询,返回作为第一次检查尚未从message_threads表读取的消息,和/或如果已读取,则仅显示最近10天的消息 间隔为10天的OR未按的顺序提供正确的记录。如果我删除了或条件,那么它就工作了。这是我当前的查询: SELECT c.customer_name, c.customer_profile_image, m.message_id, m.contract_id, m.message_sub

我有3个表(客户、消息和消息线程)。我想执行一个查询,返回作为第一次检查尚未从message_threads表读取的消息,和/或如果已读取,则仅显示最近10天的消息

间隔为10天的OR未按的顺序提供正确的记录。如果我删除了或条件,那么它就工作了。这是我当前的查询:

SELECT 
    c.customer_name, 
    c.customer_profile_image, 
    m.message_id, 
    m.contract_id, 
    m.message_subject, 
    m.last_updated, 
    mt.date_created, 
    mt.date_viewed
FROM t_customers_tenants c 
JOIN t_customers_messages m 
    ON (c.customer_id = m.tenant_id) 
JOIN t_customers_message_threads mt 
    ON (m.message_id = mt.message_id)                                               
WHERE m.customer_id = 7866                                                 
    AND mt.created_by = 'tenant'
    AND (mt.date_created > mt.date_viewed OR mt.date_created >= DATE(NOW()) - INTERVAL 10 DAY)
GROUP BY m.message_id
ORDER BY mt.message_thread_id DESC
当包含OR语句时,ORDER BY似乎不起作用

非常感谢你的帮助。我在下面包含了SQLFIDLE,正如您所看到的,最后一条记录的查看日期为0000-00-00 00:00:00,但它不会在执行查询返回的结果中显示这一点


样本数据和期望的结果确实会有帮助。@GordonLinoff当然,我会为您安排。您总是以毫无意义的方式引用
order by
。所有的数据都回来了吗?查看的日期是否大于创建的日期是测试未读数据的正确方法?我在主要描述中包含了SQL Fiddel-非常感谢@GordonLinoff请检查描述