简体   繁体   English

一个查询中的mysql 2 SELECT

[英]mysql 2 SELECT in one query

I want these two SELECT queries to give one result and then i want to order it by date_time 我希望这两个SELECT查询给出一个结果,然后我想按date_time对其进行排序

SELECT 1: 选择1:

"SELECT * FROM `table` WHERE `f_id` = '$uid' AND `t_id` = '$pid'"

SELECT 2: 选择2:

"SELECT * FROM `table` WHERE `f_id` = '$pid' AND `t_id` = '$uid'"

I tried to put it in one WHERE clause but it's impossible, I also tried to put them in sub-query but that made SELECT 2 to join right SELECT 1 :/ I want ONE output array of these two queries, ordered by date_time . 我试图将它放在一个WHERE子句中,但是这是不可能的,我也试图将它们放在子查询中,但是这使SELECT 2可以连接到SELECT 1://我希望这两个查询的一个输出数组按date_time排序。 How is it possible? 这怎么可能?

SELECT * FROM `table` WHERE `f_id` = '$uid' AND `t_id` = '$pid'
UNION ALL
SELECT * FROM `table` WHERE `f_id` = '$pid' AND `t_id` = '$uid'

EDIT: 编辑:

Er... sorry, I didn't see it is the same table. 嗯...抱歉,我没看到它是同一张桌子。

SELECT * FROM table
  WHERE (`f_id` = '$uid' AND `t_id` = '$pid') OR (`f_id` = '$pid' AND `t_id` = '$uid')
  ORDER BY date_time

What about: 关于什么:

SELECT 
    * 
FROM 
    `table` 
WHERE 
    (`f_id` = '$uid' AND `t_id` = '$pid') OR 
    (`f_id` = '$pid' AND `t_id` = '$uid')
ORDER BY 
    `date_time` DESC

try this 尝试这个

   SELECT * FROM 

      ( select * FROM `table` WHERE `f_id` = '$uid' AND `t_id` = '$pid'
       union all 
        SELECT * FROM `table` WHERE `f_id` = '$pid' AND `t_id` = '$uid' )t

   ORDER BY date_time

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

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