我有一个名为 value_record 的表格,如下所示
log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DB.URL=jdbc:sqlserver://172.16.0.201:1433;databaseName=databaseone;autoReconnect=true
log4j.appender.DB.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
log4j.appender.DB.user=username
log4j.appender.DB.password=password$123
log4j.appender.DB.sql=INSERT INTO usage_fact(accessed_date,accessed_item_id,user_id,tenant_id,log_level) VALUES('%d{yyyy-MM-dd HH:mm:ss}','%X{accessed_item_id}','%X{user_id}','%X{tenant_id}','%p')
log4j.appender.DB.layout=org.apache.log4j.PatternLayout
...
id | record_id | value | serverTime
1 | 20 | 0 | 2016-04-19 04:51:16
2 | 20 | 0 | 2016-04-19 04:40:19
3 | 20 | 0 | 2016-04-19 04:27:34
4 | 20 | 70 | 2016-04-19 04:14:24
5 | 20 | 50 | 2016-04-19 04:02:21
输出:
select value from value_record
where record_id in(20)
order by serverTime desc limit 1;
但预期输出为:
0
表示如果当前值为零,则查询返回前一行而非零,并显示非零值。
如何在mysql中实现这一点?
答案 0 :(得分:2)
您需要过滤不同于0的值,并且您不需要IN
子句:
SELECT
`value`
FROM
value_record
WHERE
record_id = 20
AND `value` > 0
ORDER BY
serverTime DESC
LIMIT 1;
答案 1 :(得分:1)
SELECT value
FROM value_record
WHERE record_id IN(20)
AND value > 0
ORDER BY serverTime LIMIT 1;