对联合函数的查询使用通用顺序

时间:2016-02-22 07:35:14

标签: mysql sql

朋友我在下面的查询中使用了两个具有相同概念的查询我使用了两个按函数排序而不是我想通过函数为整个查询只使用相同的属性编写一个命令date_time DESC可以在mysql中使用

(SELECT * FROM `chats` WHERE `chat_from` = '18' AND `chat_to` = '13' ORDER BY `date_time` DESC ) 
UNION 
(SELECT * FROM `chats` WHERE `chat_from` = '13' AND `chat_to` = '18' ORDER BY `date_time` DESC )

3 个答案:

答案 0 :(得分:2)

试试这个

SELECT * FROM `chats` 
WHERE `chat_from` IN(18,13) 
AND `chat_to`IN(18,13) 
ORDER BY `date_time` DESC 

答案 1 :(得分:1)

select * from 
(
 SELECT * FROM `chats` WHERE `chat_from`in ('18','13') and `chat_to` in ('18','13')  
)
t1 ORDER BY `date_time` DESC 

答案 2 :(得分:0)

你可以这样做:

SELECT * FROM `chats` 
WHERE 
    (
        `chat_from` = '18' 
        AND `chat_to` = '13'
    ) 
    OR
    (
        `chat_from` = '13' 
        AND `chat_to` = '18' 
    ) 
ORDER BY `date_time` DESC

如果您需要它们,那么您可以使用:

SELECT DISTINCT * FROM `chats`...