dtotwai720621 2013-08-28 21:38
浏览 62
已采纳

MySQL 3表单数据查询

I'm trying to get data from 3 tables in one query. Those tables are for storing and displaying user messages. Each user can log in and request all his inbox messages. The user id is avaliable from the PHP session and parameter type=1 gets passed in the php request which means the request to show inbox (type=2 would be info messages etc).

First table called email has id, subject, body and date

Second table called email_routing has message_id, sender_email_id, receiver_email_id, basket, type. The message_id refers to id of the message in email table.

Sender and receiver id refers to email ids from the third table that called people_emails. It consist of id(that what sender and receiver email id refer to), internal_user_id (that's what is avaliable from PHP session), email and people_id (each person may have several emails in the contact book so I store people and their emails in separate tables, but this last column if not of our interest here).

So I can figure out how I would do my queries in sequence and just for one row result:

Fist I would get user email id based on his user_id from PHP session:

$X = SELECT `id` from `people_emails` WHERE `internal_user_id`=$_conf[user]->id;

$X is not a proper coding, let's say it is a variable I would eventually get.

Then I can query the email_routing table to all the messages addressed to that user:

$Y = SELECT `message_id`, `sender_email_id`, `receiver_email_id` FROM `email_routing` WHERE `receiver_email_id`=$X[id] and `type`=$_REQUEST[type];

Having list of the message_id's I can now query the email table for the messages content:

$R = SELECT `subject`, `body`, `date` FROM `email` WHERE `id`=$Y[message_id];

Ok, then I need to "translate" the "from" addresses from its id which I got from email_routing table, so it is:

$Z_from = SELECT `email` FROM `people_emails` WHERE `id`=$X[sender_email_id];

I now have all the data for one row. Then I need to put all together which will be

$Z_from + $R[subject] + $R[body] + $R[date]

I need to have it as one list of results which then gets passed to bootstrap datatables plugin for displaying and searching. I can't seem to figure that one out. Please help.

  • 写回答

1条回答 默认 最新

  • dt250827 2013-08-28 21:52
    关注

    Your queries, reformatted:

    $X:  SELECT p.id
           FROM people_emails p 
          WHERE p.internal_user_id = $_conf[user]->id;
    
    $Y:  SELECT r.message_id
              , r.sender_email_id
              , r.receiver_email_id
           FROM email_routing r 
          WHERE r.receiver_email_id = $X[id] 
            AND r.type = $_REQUEST[type];
    
    $R:  SELECT e.subject
              , e.body
              , e.date
           FROM email e
          WHERE e.id = $Y[message_id];
    
    $F:  SELECT s.email
           FROM people_emails s 
          WHERE s.id = $Y[sender_email_id]
    

    To return these columns:

    $F + $R[subject] + $R[body] + $R[date]
    

    The queries can be combined like this:

     SELECT s.email AS sender_email
          , e.subject
          , e.body
          , e.date
       FROM people_emails p 
       JOIN email_routing r 
         ON r.receiver_email_id = p.id
        AND r.type = $_REQUEST[type]
       JOIN email e
         ON e.id = r.message_id
       JOIN people_emails s
     ON s.id = r.sender_email_id
      WHERE p.internal_user_id = $_conf[user]->id;
    

    FOLLOWUP

    To add the sender full_name from the people table, we can add a join to that table, and add the column to the SELECT list:

    $N = SELECT n.full_name
           FROM people n 
          WHERE n.id = (SELECT s.people_id
                          FROM people_emails s 
                         WHERE s.email = :sender_email
                       )
    

    We already have :sender_email from the query above, it's the value returned from s.email in the SELECT list, so we can just add a join to the people table:

     SELECT s.email     AS sender_email
          , n.full_name AS sender_full_name
          , e.subject
          , e.body
          , e.date
       FROM people_emails p 
       JOIN email_routing r
         ON r.receiver_email_id = p.id
        AND r.type = $_REQUEST[type]
       JOIN email e
         ON e.id = r.message_id
       JOIN people_emails s
     ON s.id = r.sender_email_id
       JOIN people n
         ON n.id = s.people_id
      WHERE p.internal_user_id = $_conf[user]->id;
    

    (I can't really test this, since I don't have your table definitions or sample data.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制