MYSQL查询:查询显示倒数第二行的数据

时间:2018-01-02 02:28:41

标签: mysql sql

我想问一下如何显示第二行到最后一行的数据?

+----+------------------+-------+
| id |   nama_makanan   | harga |
+----+------------------+-------+
|  1 | Ayam Katsu       | 18000 |
|  2 | Udon Daging Sapi | 26000 |
|  3 | Mie Ramen Gila   | 24000 |
|  4 | Cah Kangkung     | 16000 |
|  5 | Sayur Nangka     | 10000 |
+----+------------------+-------+

例如,如果使用这样的限制:

SELECT * FROM tbl_makanan ORDER BY id ASC LIMIT 1,4

结果:

+----+------------------+-------+
| id |   nama_makanan   | harga |
+----+------------------+-------+
|  2 | Udon Daging Sapi | 26000 |
|  3 | Mie Ramen Gila   | 24000 |
|  4 | Cah Kangkung     | 16000 |
|  5 | Sayur Nangka     | 10000 |
+----+------------------+-------+

但这是静态的,如果表中的数据非常多,该怎么办?如何获取第二个数据直到最后一个数据?

4 个答案:

答案 0 :(得分:1)

使用左连接

获得所需结果的另一种方法
.modal-header {
padding: 15px;
border-bottom: 1px solid #e5e5e5;
}

.modal-dialog{
      width: 600px;
    margin: 30px auto;
}

.modal-content{
  
  position: relative;
    background-color: #fff;
    -webkit-background-clip: padding-box;
    background-clip: padding-box;
    border: 1px solid #999;
    border: 1px solid rgba(0,0,0,.2);
    border-radius: 6px;
    outline: 0;
    -webkit-box-shadow: 0 3px 9px rgba(0,0,0,.5);
    box-shadow: 0 3px 9px rgba(0,0,0,.5);
}

.modal-title {
    margin: 0;
    line-height: 1.42857143;
}

h1 {
  text-align: center;
  font-family: Tahoma, Arial, sans-serif;
  color: beige;
  margin: 80px 0;
}

.box {
  width: 40%;
  margin: 0 auto;
  background: red;
  padding: 35px;
  border: 2px solid #fff;
  border-radius: 20px/50px;
  background-clip: padding-box;
  text-align: center;
}

.button {
  font-size: 1em;
  padding: 10px;
  color: #fff;
  border: 2px solid blue;
  border-radius: 20px/50px;
  text-decoration: none;
  cursor: pointer;
  transition: all 0.3s ease-out;
}
.button:hover {
  background: blue;
}

.overlay {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.7);
  transition: opacity 500ms;
  visibility: hidden;
  opacity: 0;
}
.overlay:target {
  visibility: visible;
  opacity: 1;
}



.popup h2 {
  margin-top: 0;
  color: #333;
  font-family: Tahoma, Arial, sans-serif;
}
.popup .close {
  position: absolute;
  top: 20px;
  right: 30px;
  transition: all 200ms;
  font-size: 30px;
  font-weight: bold;
  text-decoration: none;
  color: #333;
}
.popup .close:hover {
  color: #06D85F;
}
.popup .content {
  max-height: 30%;
  overflow: auto;
}

@media screen and (max-width: 700px){
  .box{
    width: 70%;
  }
  .popup{
    width: 70%;
  }
}

    

DEMO

答案 1 :(得分:0)

只需使用非常大的“4”值即可。这适用于大多数表:

SELECT *
FROM tbl_makanan
ORDER BY id ASC
LIMIT 1, 999999999;

答案 2 :(得分:0)

你可以使用它(这是一个有效的Fiddle):

SELECT id, nama_makanan, harga FROM tbl_makanan WHERE id < (SELECT MAX(`id`) FROM tbl_makanan) ORDER BY id DESC LIMIT 1

答案 3 :(得分:0)

希望这对你有用.. :)

SELECT *
FROM tbl_makanan
ORDER BY id ASC 
OFFSET 1 ROWS