davidgereb
davidgereb

Reputation: 119

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 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'"

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. How is it possible?

Upvotes: 0

Views: 6056

Answers (3)

chrislondon
chrislondon

Reputation: 12041

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

Upvotes: 3

echo_Me
echo_Me

Reputation: 37243

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

Upvotes: 0

Voitcus
Voitcus

Reputation: 4456

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

Upvotes: 5

Related Questions