如何从表中的两个查询中获取常用值并将其与MySql中的另一个表连接?

时间:2015-03-20 07:03:10

标签: mysql join group-by

我有两张桌子。一个是订单,另一个是用户。我想在订单表格上运行两个不同的查询,并希望获得常用值并将其与用户表格结合使用。

    orders
    ======
    +----+---------+------------+------------+--------------+
    | id | user_id | receive_id | arrival_id | departure_id |
    +----+---------+------------+------------+--------------+
    | 1  |   1     |     1      |    null    |      5       |
    +----+---------+------------+------------+--------------+
    | 2  |   2     |     1      |      3     |      6       |
    +----+---------+------------+------------+--------------+
    | 3  |   3     |     1      |    null    |      7       |
    +----+---------+------------+------------+--------------+
    | 4  |   1     |     3      |      5     |    null      |
    +----+---------+------------+------------+--------------+
    | 5  |   3     |     3      |      6     |    null      |
    +----+---------+------------+------------+--------------+
    | 6  |   4     |     3      |      7     |    null      |
    +----+---------+------------+------------+--------------+
    | 7  |   1     |     4      |      7     |      9       |
    +----+---------+------------+------------+--------------+
    users
    =====
    +----+---------+
    | id |   name  |
    +----+---------+
    | 1  |  Shaon  |
    +----+---------+
    | 2  |  Rabu   |
    +----+---------+
    | 3  |  Asha   | 
    +----+---------+

第一次查询..

SELECT DISTINCT user_id from orders where receive_id = 1 and departure_id != 'null'
Its result will be..
1 2 3

第二次查询..

SELECT DISTINCT user_id from orders where receive_id = 3 and arrival_id != 'null'
Its result will be..
1 3 4

我希望从这些查询获得共同的价值

That is ..
1 3

When I Join it to users table then result will be
_id__name__arrival_id__departure_id
 1  Shaon    null           5
 3  Asha     null           7
But now I want to see..
_id__name__arrival_id__departure_id
 1  Shaon     5             5
 3  Asha      6             7
on arrival_id row the value from where 
receive_id = 3 there's arrival_id as distinct user_id

I think code will be like bellow. But It's not working.
SELECT DISTINCT user_id from orders where receive_id = 1 and departure_id != 'null'
COMMON
SELECT DISTINCT user_id from orders where receive_id = 3 and arrival_id != 'null'
JOIN users on (users.id = orders.user_id)

3 个答案:

答案 0 :(得分:3)

通过将一个查询作为另一个查询作为子查询,您将得到所需的内容 像这样的东西,

SELECT DISTINCT user_id from orders
JOIN users on (users.id = orders.user_id)
where receive_id = 1 and departure_id != 'null'
and user_id in (SELECT DISTINCT user_id from orders where receive_id = 3 and arrival_id != 'null')

希望这会有所帮助。

答案 1 :(得分:0)

SELECT DISTINCT user_id from orders JOIN 
users on (users.id = orders.user_id) where receive_id = 3 and 
arrival_id != 'null' AND user_id IN (SELECT DISTINCT user_id from 
orders   where receive_id = 1 and departure_id != 'null');

您可以使用此查询来获取记录

答案 2 :(得分:0)

尝试此查询,

select orders.id, users.id from orders inner join users 
on orders.user_id = users.id