mysql 2在一个查询中选择SELECT

时间:2013-03-24 20:33:39

标签: php mysql select where

我希望这两个SELECT查询给出一个结果然后我想按date_time

排序

SELECT 1:

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

SELECT 2:

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

我试着将它放在一个WHERE子句中,但这是不可能的,我也尝试将它们放在子查询中但是这使得SELECT 2加入了正确的SELECT 1:/我想要这两个查询的一个输出数组,排序方式date_time。 怎么可能?

3 个答案:

答案 0 :(得分:5)

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

编辑:

呃...对不起,我没有看到它是同一张桌子。

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

答案 1 :(得分:3)

怎么样:

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

答案 2 :(得分:0)

试试这个

   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