dqxz96998 2013-06-13 16:26
浏览 21
已采纳

使用PHP从多个表中查询MySQL数据

I'm working on a database for monitoring sportinjuries. I have 2 tables, one is called injury the other one injury_list.

Injury looks like this:

-----------------------------------------------------------------------
injury_id | name   | body_part  | first_mention | last_changed | status
-----------------------------------------------------------------------
|   2     |  Ben   |   arm      |   2013-06-08  |   2013-06-13 |   0  |
|   3     |  Rick  |   knee     |   2013-05-10  |   2013-06-12 |   0  |
|   4     |  Esther|   ankle    |   2013-05-26  |   2013-06-12 |   1  |
-----------------------------------------------------------------------

and then we have injury_list which I use to store the updates from Physiotherapists and coaches

-----------------------------------------------------------------------
  list_id | injury_id | Comments               | trend | comment_added
-----------------------------------------------------------------------
|   1     |     2     | Complains a lot wo.... |   1   |   2013-06-01 |
|   2     |     2     | Gets a little bit be.. |   3   |   2013-06-08 |
|   3     |     2     | no changes so far..... |   2   |   2013-06-13 |   
|   4     |     4     | aches a lot, send t... |   1   |   2013-06-01 |
|   5     |     4     | Got a lot worse ne.... |   1   |   2013-06-08 |
|   6     |     4     | no changes so far..... |   2   |   2013-06-13 |   
-----------------------------------------------------------------------

Trend is used to show if the injury got worse (1), better(2) or no change(3)

I have an overview off all injuries where I only use the INJURY table and a detailed page per injury, where I use information from both tables this all works fine.

now I want the TREND to show on the main page in the overview, and as you can understand I only want the latest trend (based on comment_added). I tried several several queries but I can't seem to understand how to righteously call the data.

I'm not realy good with joins, and I actually don't know if that is the solution here, I hope someone can help me out:

$result = mysqli_query($con,"
SELECT b.injury_id 
     , bl.injury_id b.name
     , b.body_part
     , b.first_mention
     , b.last_changed
     , b.status 
  FROM injury b
  JOIN injury_list bl 
    ON bl.injury_id = b.injury_id 
 ORDER 
    BY status ASC
     , last_changed DESC;
");

thanks in advance for thinking with me.

  • 写回答

3条回答 默认 最新

  • dongzha0813 2013-06-13 17:35
    关注

    ...and here's a third way, a simple LEFT JOIN to find the latest row to eliminate the subquery;

    SELECT i.*,il1.*
    FROM injury i
    JOIN injury_list il1
      ON i.injury_id = il1.injury_id
    LEFT JOIN injury_list il2
      ON i.injury_id = il2.injury_id
     AND il1.comment_added < il2.comment_added
    WHERE il2.injury_id IS NULL
    

    An SQLfiddle to test with.

    EDIT: A quick (and, I realize not entirely easy to follow) explanation; The first join is a totally normal join to get an injury and a corresponding entry in injury_list. I then LEFT JOIN to injury list again to see if there exists a newer entry. If not, the left join will leave all fields in il2 NULL (ie, non existant) and we should show the row we just built. If there exists a newer entry, the fields in il2 will have the data from the newer entry, and in that case the row should not be shown.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上