如何获取具有某些条件的SQL表的最后一行

[英]How to get the last line of a SQL table with some conditions


I'm having some problem with the following bit of code in my BASH script. I want to get data from my_table. I want to get the LAST(timestamp) from the last line which has the 1 value in the sensor_id column. So I looked over here for some help.

我的BASH脚本中的以下代码有问题。我想从my_table获取数据。我想从传感器列中具有1值的最后一行获取LAST(时间戳)。所以我在这里寻求帮助。

  dbq="SELECT LAST(timestamp) FROM table_list.my_table WHERE sensor_id=1"
    mysql -uUSER -p'PASSWORD' >>myvar << EOF
    $dbq
    EOF

    echo $myvar

Once I get this information, I want to echo it out with bash.

一旦我得到这些信息,我想用bash回应它。

The error I recieve is:

我收到的错误是:

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(timestamp) FROM table_list.my_table WHERE sensor_id=1' at line 1

第1行的错误1064(42000):SQL语法中有错误;查看与您的MySQL服务器版本对应的手册,以便在第1行的'(timestamp)FROM table_list.my_table WHERE sensor_id = 1'附近使用正确的语法

1 个解决方案

#1


There is no LAST() function in mysql.

mysql中没有LAST()函数。

Can your try select as below :

您可以尝试选择如下:

dbq="SELECT timestamp FROM 
        table_list.my_table WHERE sensor_id=1 
            ORDER BY timestamp DESC LIMIT 1"
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2015/05/10/13d0e109a876d9617534f21a000360d1.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告