Mysql查询两个表连接条件

时间:2017-09-28 05:38:46

标签: php mysql

我有两个表,ready_for_client和ready_to_ship。两个表的结构几乎相同。

    id (int)
    client_id (int)
    truck_id (int)
    driver_id (int)
    list (varchar 50)
    is_done (tinyint 1)

ready_for_client有rts_id (varchar 32),ready_to_ship有rfc_id (varchar 32)

我想要的是三个不同的查询,第一个查询rts_id和rfc_id相同的两个表。我有第一个,它有效。

    SELECT rfc.id AS Ready_For_Client_id, rfc.rts_id, t.Truck_Number, u.Name AS User_Name, c.Name AS Client_Name FROM ready_for_client rfc
      INNER JOIN trucks t ON rfc.truck_id = t.id
      INNER JOIN users u ON rfc.driver_id = u.id
      INNER JOIN clients c ON rfc.client_id = c.id
      INNER JOIN ready_to_ship rts ON rfc.rts_id = rts.rfc_id
    WHERE rfc.is_done = 0

这会生成一条记录。第二个和第三个查询我想分别选择不共享rfc_id和rts_id的两个表的所有其他记录。

我想过拥有相同的查询,但是在第四个INNER JOIN中它不等于。

    SELECT rfc.id AS Ready_For_Client_id, rfc.rts_id, t.Truck_Number, u.Name AS User_Name, c.Name AS Client_Name FROM ready_for_client rfc
      INNER JOIN trucks t ON rfc.truck_id = t.id
      INNER JOIN users u ON rfc.driver_id = u.id
      INNER JOIN clients c ON rfc.client_id = c.id
      INNER JOIN ready_to_ship rts ON rfc.rts_id != rts.rfc_id
    WHERE rfc.is_done = 0

但是这会使记录加倍,还包括具有共享rts_id和rfc_id的一条记录。

有谁知道如何设置第二个和第三个查询来完成我需要的工作?

- 编辑 -

ready_for_client

    +----+-----------+----------+-----------+---------------------------+----------------------------------+---------+
    | id | client_id | truck_id | driver_id | list                      | rts_id                           | is_done |
    +----+-----------+----------+-----------+---------------------------+----------------------------------+---------+
    |  1 |         1 |        2 |         7 | 175                       | 2bcd069351002c4d92ba151ae67212ad |       0 |
    |  2 |         2 |        2 |         7 | 231                       | 95e906dec3c83b1c9bfbe3f0d6920aaf |       0 |
    |  3 |         2 |        1 |         7 | 88,89,90,91               | e82ac070985e21ced4fb203c515b1618 |       0 |
    |  4 |         3 |        1 |         7 | 37,249,38,39,40,41,109,42 | d21b5ac4a4da0e57b85a464c3a6fd511 |       0 |
    +----+-----------+----------+-----------+---------------------------+----------------------------------+---------+

ready_to_ship

    +----+-----------+----------+-----------+-------+----------------------------------+---------+
    | id | client_id | truck_id | driver_id | list  | rfc_id                           | is_done |
    +----+-----------+----------+-----------+-------+----------------------------------+---------+
    |  1 |         3 |        1 |         7 | 1     | 10e938e49a67117273683535cebbe8cc |       0 |
    |  2 |         3 |        1 |         7 | 2,3,4 | d21b5ac4a4da0e57b85a464c3a6fd511 |       0 |
    +----+-----------+----------+-----------+-------+----------------------------------+---------+

第二个查询的输出,尝试从ready_for_client中选择所有其他记录:

    +---------------------+----------------------------------+--------------+------------+----------------------+
    | Ready_For_Client_id | rts_id                           | Truck_Number | User_Name  | Client_Name          |
    +---------------------+----------------------------------+--------------+------------+----------------------+
    |                   1 | 2bcd069351002c4d92ba151ae67212ad |           20 | driver     | client name          |
    |                   1 | 2bcd069351002c4d92ba151ae67212ad |           20 | driver     | client name          |
    |                   2 | 95e906dec3c83b1c9bfbe3f0d6920aaf |           20 | driver     | client name          |
    |                   2 | 95e906dec3c83b1c9bfbe3f0d6920aaf |           20 | driver     | client name          |
    |                   3 | e82ac070985e21ced4fb203c515b1618 |           10 | driver     | client name          |
    |                   3 | e82ac070985e21ced4fb203c515b1618 |           10 | driver     | client name          |
    |                   4 | d21b5ac4a4da0e57b85a464c3a6fd511 |           10 | driver     | client name          |
    +---------------------+----------------------------------+--------------+------------+----------------------+

它应该只适用于Read_For_Client_id 1 2和3.没有id 4且不会加倍1 2和3。

1 个答案:

答案 0 :(得分:1)

您想要找到ID不相等的行,但您需要避免行的乘法。这里有2种技巧。

使用LEFT JOIN但不包括连接存在的那些

SELECT
      rfc.id AS Ready_For_Client_id
    , rfc.rts_id
    , t.Truck_Number
    , u.Name AS User_Name
    , c.Name AS Client_Name
FROM ready_for_client rfc
INNER JOIN trucks t ON rfc.truck_id = t.id
INNER JOIN users u ON rfc.driver_id = u.id
INNER JOIN clients c ON rfc.client_id = c.id
LEFT JOIN (
      SELECT
            rfc.id
      FROM ready_for_client rfc
      INNER JOIN ready_to_ship rts ON rfc.rts_id = rts.rfc_id
      WHERE rfc.is_done = 0
      ) d ON rfc.rts_id = d.id
WHERE d.id IS NULL
;

使用NOT EXISTS。确定当前行是否在另一个表中匹配,如果它确实忽略该行。

SELECT
      rfc.id AS Ready_For_Client_id
    , rfc.rts_id
    , t.Truck_Number
    , u.Name AS User_Name
    , c.Name AS Client_Name
FROM ready_for_client rfc
INNER JOIN trucks t ON rfc.truck_id = t.id
INNER JOIN users u ON rfc.driver_id = u.id
INNER JOIN clients c ON rfc.client_id = c.id
WHERE rfc.is_done = 0
AND NOT EXISTS (
      SELECT NULL
      FROM ready_to_ship rts 
      WHERE rfc.rts_id = rts.rfc_id
      ) 
;