MySQL:按开始时间排序事件,立即按结束事件

时间:2017-06-25 04:54:13

标签: mysql performance sorting events alternate

我的数据库中有这样的事件数据:

 time | obj | type
-------------------
 0012   23    start
 0014   24    start
 0023   25    start
 0034   23    end
 0056   26    start
 0058   23    start   (reusing obj 23)
 0060   25    end
 0100   24    end
 0101   23    end
 0107   26    end
 ...

我想通过开始事件时间对其进行排序,然后是同一对象的结束事件:

 time | obj | type
-------------------
 0012   23    start
 0034   23    end
 0014   24    start
 0100   24    end
 0023   25    start
 0060   25    end
 0056   26    start
 0107   26    end
 0058   23    start
 0101   23    end
 ...

到目前为止,我没有想出一个SQL查询来输出这样的数据。当然,我可以查询所有启动事件,然后在每行查询来自我的C API的相应结束事件后,但我有多个10.000结束事件,这看起来效率很低。

编辑:对不起,我忘了提到obj号码可以重复使用(并且有一个唯一的id行)当我第一次问这个问题时,我现在更新了样本。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

如果结束时间总是在开始时间之后,这应该有效

select time,obj,type
from t order by obj,time;

但你也可以在排序

中使用一个案例陈述
select time,obj,type
from t 
order by obj,
        case when type = 'start' then 1
        else 2
        end;

如果obj被重用,那么棘手的一点是在开始命令之前将开始类型的id分配给结束类型

select t1.time,t1.obj,t1.type, t1.id,
         case when t2.id is null then t1.id
         else t2.id 
         end as startid
from t t1
left join t t2 on t2.obj = t1.obj and t2.id = (select max(t.id) from t where t.obj = t1.obj and t.id < t1.id and t1.type = 'end')
order by startid,id

+------+------+-------+----+---------+
| time | obj  | type  | id | startid |
+------+------+-------+----+---------+
|   12 |   23 | start |  1 |       1 |
|   34 |   23 | end   |  4 |       1 |
|   14 |   24 | start |  2 |       2 |
|  100 |   24 | end   |  8 |       2 |
|   23 |   25 | start |  3 |       3 |
|   60 |   25 | end   |  7 |       3 |
|   56 |   26 | start |  5 |       5 |
|  107 |   26 | end   | 12 |       5 |
|   58 |   23 | start |  6 |       6 |
|  101 |   23 | end   |  9 |       6 |
|  102 |   23 | start | 10 |      10 |
|  103 |   23 | end   | 11 |      10 |
+------+------+-------+----+---------+
12 rows in set (0.00 sec)

答案 1 :(得分:1)

您可以简单地依赖事件类型的顺序。因为e在s之前,然后排序desc。

    <!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/reset.css" type="text/css" media="all">
<link rel="stylesheet" href="css/style.css" type="text/css" media="all">
<script type="text/javascript" src="js/jquery-1.4.2.min.js" ></script>
<script type="text/javascript" src="js/cufon-yui.js"></script>
<script type="text/javascript" src="js/cufon-replace.js"></script>
<script type="text/javascript" src="js/Myriad_Pro_300.font.js"></script>
<script type="text/javascript" src="js/Myriad_Pro_400.font.js"></script>
<script type="text/javascript" src="js/script.js"></script>
<!--[if lt IE 7]>
<link rel="stylesheet" href="css/ie6.css" type="text/css" media="screen">
<script type="text/javascript" src="js/ie_png.js"></script>
<script type="text/javascript">ie_png.fix('.png, footer, header nav ul li a, .nav-bg, .list li img');</script>
<![endif]-->
<!--[if lt IE 9]><script type="text/javascript" src="js/html5.js"></script><![endif]-->
</head>
<body id="page1">
<!-- START PAGE SOURCE -->

<div class="wrap">
  <header>
    <div class="container">
    <h1><a href="#">Schooling india</a></h1>

      <nav>
        <ul>
          <li class="current"><a href="index.html" class="m1">Home Page</a></li>

          <li>
          <div class="dropdown"><a href="about-us.html" class="m2 dropbtn">About Us</a>
          <div class="dropdown-content">
                <a href="#">Link 1</a>
                 <a href="#">Link 2</a>
                  <a href="#">Link 3</a>
          </div>
          </div></li>

          <li>
          <div class="dropdown"><a href="about-us.html" class="m3 dropbtn">Admissions</a>
          <div class="dropdown-content">
                <a href="#">Link 1</a>
                 <a href="#">Link 2</a>
                  <a href="#">Link 3</a>
          </div>
          </div></li>

          <li>
          <div class="dropdown"><a href="about-us.html" class="m4 dropbtn">Rules</a>
          <div class="dropdown-content">
                <a href="#">Link 1</a>
                 <a href="#">Link 2</a>
                  <a href="#">Link 3</a>
          </div>
          </div></li>
          <li>
          <div class="dropdown"><a href="about-us.html" class="m5 dropbtn">Info Corner</a>
          <div class="dropdown-content">
                <a href="#">Link 1</a>
                 <a href="#">Link 2</a>
                  <a href="#">Link 3</a>
          </div>
          </div></li>


          <li>
          <div class="dropdown"><a href="about-us.html" class="m6 dropbtn">Achievements</a>
          <div class="dropdown-content">
                <a href="#">Link 1</a>
                 <a href="#">Link 2</a>
                  <a href="#">Link 3</a>
          </div>
          </div></li>



          <li>
          <div class="dropdown"><a href="about-us.html" class="m7 dropbtn">Gallery</a>
          <div class="dropdown-content">
                <a href="#">Link 1</a>
                 <a href="#">Link 2</a>
                  <a href="#">Link 3</a>
          </div>
          </div></li>



          <li>
          <div class="dropdown"><a href="about-us.html" class="m8 dropbtn">Contact</a>
          <div class="dropdown-content">
                <a href="#">Link 1</a>
                 <a href="#">Link 2</a>
                  <a href="#">Link 3</a>
          </div>
          </div></li>


        </ul>
      </nav>
      <form action="#" id="search-form">
        <fieldset>
          <div class="rowElem">
            <!-- <input type="text" placeholder="Search">
            <a href="#">Search</a></div> -->

            <script type="text/javascript">
                    var tmonth=new Array("January","February","March","April","May","June","July","August","September","October","November","December");

                    function GetClock(){
                    var d=new Date();
                    var nmonth=d.getMonth(),ndate=d.getDate(),nyear=d.getFullYear();

                    var nhour=d.getHours(),nmin=d.getMinutes(),nsec=d.getSeconds(),ap;

                    if(nhour==0){ap=" AM";nhour=12;}
                    else if(nhour<12){ap=" AM";}
                    else if(nhour==12){ap=" PM";}
                    else if(nhour>12){ap=" PM";nhour-=12;}

                    if(nmin<=9) nmin="0"+nmin;
                    if(nsec<=9) nsec="0"+nsec;

                    document.getElementById('clockbox').innerHTML=""+tmonth[nmonth]+" "+ndate+", "+nyear+"  "+nhour+":"+nmin+":"+nsec+ap+"";
                    }

                    window.onload=function(){
                    GetClock();
                    setInterval(GetClock,1000);
                    }
              </script>
          <div id="clockbox"></div>

           <!--  <script > var date=new Date();
            document.write(date);</script> -->
        </fieldset>
      </form>
    </div>
  </header>
  <div class="container">
    <aside>
      <h3>Categories</h3>
      <ul class="categories">
        <li><span><a href="#">Programs</a></span></li>
        <li><span><a href="#">Student Info</a></span></li>
        <li><span><a href="#">Teachers</a></span></li>
        <li><span><a href="#">Admissions</a></span></li>
        <li><span><a href="#">Administration</a></span></li>
        <li><span><a href="#">Basic Information</a></span></li>
        <li><span><a href="#">Vacancies</a></span></li>
        <li class="last"><span><a href="#">Academic Calendar</a></span></li>
      </ul>
      <form action="#" id="newsletter-form">
        <fieldset>
          <div class="rowElem">
            <h2>School lettter</h2>
            <input type="email" value="Enter Your Email Here" onFocus="if(this.value=='Enter Your Email Here'){this.value=''}" onBlur="if(this.value==''){this.value='Enter Your Email Here'}" >
            <div class="clear"><a href="#" class="fleft">Unsubscribe</a><a href="#" class="fright">Submit</a></div>
          </div>
        </fieldset>
      </form>
      <h2>Academic <span>Calender</span></h2>
      <ul class="news">
        <li><strong>June 30, 2017</strong>
          <h4><a href="#">1<sup>st</sup> parents-teacher meeting</a></h4>
          parents are requested to be present by 11:00 am sharp </li>
        <li><strong>June 14, 2017</strong>
          <h4><a href="#">Start of new term</a></h4>
         Students should be present on the 1st day timings are from 8:00 am to 2:00 pm </li>
        <li><strong>May 29, 2017</strong>
          <h4><a href="#">Result declaration</a></h4>
          Report card distribution will start at 9:00pm</li>
        <li>
          <h4><a href="#">See more...</a></h4></li>
      </ul>
    </aside>
    <section id="content">
      <div id="banner">
        <h2>Educating <span>India <span>Since 1992</span></span></h2>
      </div>
      <div class="inside">
        <h2>Recent <span>News</span></h2>
        <ul class="list">
          <li><span><img src="images/icon1.png"></span>
            <h4>About Us</h4>
            <p>This is the region wherein you can give a brief description of your school and its pros. Maybe you can give a read more option too.</p>
          </li>
          <li><span><img src="images/icon2.png"></span>
            <h4>Our Branches</h4>
            <p>We have branches in following areas<br><ul style="color: #008cc4">
            <li><strong>Branch 1</strong></li>
            <li><strong>Branch 2</strong></li>
            <li><strong>Branch 3</strong></li>
            <li><strong>Branch 4</strong></li></ul></p>
          </li>
          <li class="last"><span><img src="images/icon3.png"></span>
            <h4>Student’s Life</h4>
            <p>In this region you can include the alumini students and their profile, recent performance of students in competitions or olympiads</p>
          </li>
        </ul>
        <h2>Move Forward <span>With Your Education</span></h2>
        <p><span class="txt1">XYZ school</span>,our school follows the moto of jai Jagat: praise the world. We strive to provide education for everyone</p>
        <div class="img-box"><img src="images/1page-img.jpg">Harbouring students from all over the city, with its distingused faculty and staff, we strive to provide excellent education with appropriate details and diversity in knowledge to prrepare better citizens fro a better tomorrow. Having eastablished in 1992, our schoolhas crossed many milestones and from time to time proved its worth by winning competitions and olympiad all over the world</div>
        <p class="p0"></p>
      </div>
    </section>
  </div>
</div>
<footer>
  <div class="footerlink">
    <p class="lf">Copyright &copy; 2017 <a href="#">SiteName</a> - All Rights Reserved</p>
    <p class="rf">Design by <a href="#">Rohit Jaiswal</a></p>
    <div style="clear:both;"></div>
  </div>
</footer>
<script type="text/javascript"> Cufon.now(); </script>
<!-- END PAGE SOURCE -->
</body>
</html>