我可以简短这个mysql查询吗?

时间:2015-03-11 11:56:20

标签: mysql

我在我的脚本中使用此查询:

SELECT *
FROM `login_messages`
WHERE  `user_id` = :user_id
      AND `to` = :to
      AND (`date` BETWEEN :date AND NOW())
   OR     `user_id` = :to
      AND `to` = :user_id
      AND (`date` BETWEEN :date AND NOW())
ORDER BY `login_messages`.`date` ASC

无论如何都要使用这部分

(
    AND (`date` BETWEEN :date AND NOW())
)

只查询一次?这在查询加载速度中不重要吗?

谢谢你们。

1 个答案:

答案 0 :(得分:1)

您可以使用IN子句

试一下

SELECT *
FROM login_messages
WHERE ((user_id ='userid1' AND TO ='to1')
    OR (user_id ='to1' AND TO ='userid1'))
  AND (`date` BETWEEN :date AND NOW())
ORDER BY `login_messages`.`date` ASC