显示仅与MySQL中的供应商有关的消息

时间:2019-01-29 09:56:52

标签: php mysql sql database

我在网站上有两种不同的用户角色。

1)供应商 2)情侣

我为此建立了一个离线消息系统。我希望当供应商进入他的收件箱时,他应该只能看到属于他的那些消息。

这是我的带有示例数据的表结构:

这是我的“线程”表:

 id | subject        | created_at          | updated_at
----------------------------------------------------------------
 42 | Hello supplier | 2019-01-14 13:45:45 | 2019-01-14 13:45:45
 43 | Interested in  | 2019-01-14 13:45:45 | 2019-01-14 13:45:45
 44 | Hi there       | 2019-01-14 13:45:45 | 2019-01-14 13:45:45
 45 | Hello          | 2019-01-14 13:45:45 | 2019-01-14 13:45:45
 46 | Hi             | 2019-01-14 13:45:45 | 2019-01-14 13:45:45

这是我的“参与者”表:

 id | thread_id | user_id  | last_read           | created_at
---------------------------------------------------------------------
 54 | 42        |   63     | 2019-01-14 13:45:45 | 2019-01-14 13:45:45
 55 | 42        |   43     | 2019-01-14 13:45:45 | 2019-01-14 13:45:45
 56 | 46        |   108    | 2019-01-14 13:45:45 | 2019-01-14 13:45:45
 57 | 46        |   11     | 2019-01-14 13:45:45 | 2019-01-14 13:45:45
 58 | 44        |    7     | 2019-01-14 13:45:45 | 2019-01-14 13:45:45
 59 | 44        |   60     | 2019-01-14 13:45:45 | 2019-01-14 13:45:45

这是我的“用户”表:

 id | name   |   role    | created_at
------------------------------------------------
 7  | ali    |  supplier | 2019-01-14 13:45:45 | 
 63 | anas   |  couple   | 2019-01-14 13:45:45 | 
 43 | zain   |  supplier | 2019-01-14 13:45:45 | 
 11 | alex   |  supplier | 2019-01-14 13:45:45 | 
 60 | ammy   |  couple   | 2019-01-14 13:45:45 |
108 | zia    |  couple   | 2019-01-14 13:45:45 | 

这是我的“供应商”表:

 id |user_id |  avatar   | business_name | Created_at
--------------------------------------------------------------
 5  |    7   |   a.jpeg  |  Abc supplier | 2019-01-14 13:45:45  
 7  |   11   |   b.jpeg  |  Bcd supplier | 2019-01-14 13:45:45 
15  |   43   |   c.jpeg  |  Def supplier | 2019-01-14 13:45:45 

这是我的“情侣”表:

 id |user_id |  avatar    | name | Created_at
------------------------------------------------------
 7  |   63   |   ab.jpeg  | Anas | 2019-01-14 13:45:45  
21  |  108   |   cd.jpeg  | Zia  | 2019-01-14 13:45:45 
 4  |   60   |   ef.jpeg  | Zia  | 2019-01-14 13:45:45 

这是我的查询

SELECT t.*, s.user_id as supplier_id, c.name, c.avatar,
       c.user_id as couple_id, u.role as currentUserRole
FROM threads t
    INNER JOIN participants p ON p.thread_id = t.id
    INNER JOIN users u
    INNER JOIN couple c ON c.user_id = p.user_id
    INNER JOIN supplier s
WHERE s.user_id = 43 AND u.id = 43

当前结果是

id |      Subject   | supplier_id | name | avatar  | couple_id | current user Role
------------------------------------------------------------------------------------
42  | Hello supplier|       43    | zain | ab.jpeg |     63    |    supplier 
44  | Hi there      |       43    | zain | cd.jpeg |    108    |    supplier 
46  | Hi there      |       43    | zain | cd.jpeg |    108    |    supplier

我想要的是仅显示属于特定配方的那些消息,但是现在它向所有用户(夫妇或供应商)显示所有消息。

1 个答案:

答案 0 :(得分:0)

尝试

SELECT 
    t.*,
    s.user_id AS supplier_id,
    c.name,
    c.avatar,
    c.user_id AS couple_id,
    u.role AS currentUserRole
FROM
    threads t
        INNER JOIN
    participants p ON p.thread_id = t.id
        INNER JOIN
    users u on p.user_id=u.id
        INNER JOIN
    couple c ON c.user_id = u.id
        INNER JOIN
    supplier s on s.user_id= u.id
WHERE
    s.user_id = 43 AND u.id = 43;