我使用以下查询根据日期之间的某些选择以前的股票, 但是如果自动递增的id不按顺序,则以下查询失败(删除一些记录时,它有间隙),
SELECT in_stock, price
FROM $tablename
WHERE id=(SELECT (id-1)
FROM $tablename
WHERE sale_date BETWEEN '$from_date' AND '$to_date'
ORDER BY entered_date ASC
LIMIT 1)
帮我选择之前的in_stock
答案 0 :(得分:1)
如果ID不符合规定,您应该使用以下内容:
SELECT in_stock, price
FROM $tablename
WHERE entered_date=(
SELECT MAX(entered_date) as max_date
FROM $tablename
WHERE
entered_date<(SELECT MIN(entered_date) as min_date
FROM $tablename
WHERE
sale_date BETWEEN '$from_date' AND '$to_date')
)
答案 1 :(得分:1)
这有效,
SELECT in_stock, price
FROM $tablename
WHERE id < (SELECT min(id)
FROM $tablename
WHERE sale_date BETWEEN '$from_date' AND '$to_date') ORDER BY id DESC LIMIT 1
答案 2 :(得分:0)
试试这个
SELECT in_stock, price
FROM $tablename
WHERE id=(SELECT (SELECT MAX(id)
FROM $tablename
WHERE id < ( SELECT MAX(id)
FROM $tablename ))
FROM $tablename
WHERE sale_date BETWEEN '$from_date' AND '$to_date'
ORDER BY entered_date ASC
LIMIT 1)
答案 3 :(得分:-1)
SELECT in_stock,price
FROM $tablename
WHERE id=(SELECT max(id) > 1
FROM $tablename
WHERE sale_date BETWEEN '$from_date' AND '$to_date'
ORDER BY entered_date ASC
LIMIT 1)