0

我需要从 base 获取最后一条记录 wherecode = variablecomplete = 0.

我使用这个 SQL:

SELECT email, date FROM users_recovery WHERE restore_key = '" . $code . "' AND complete = '0'

例如数据库:

email - code - complete - date
test@email - code1 - 0 - 1111111
test@email - code2 - 0 - 2222222

如果变量$code将是 DB 中的值之一,那么查询将完美地工作,但我还需要检查它是否是这封电子邮件的最后一条记录,但我不明白如何。

4

3 回答 3

0

使用ORDER BYwithLIMIT将结果限制为最后一条记录(ee ORDER BY 最近日期):

SELECT 
    email, 
    date 
FROM 
    users_recovery 
WHERE 
    restore_key = '" . $code . "' 
AND complete = '0'
ORDER BY 
    date DESC
LIMIT 1
于 2013-03-20T17:48:50.230 回答
0

试试这个...

SELECT b.email, b.date, b.complete, b.code
FROM   (SELECT email, 
               Max(date) AS date 
        FROM   users_recovery 
        GROUP  BY email
        WHERE  code = variable
               AND complete = 0) a 
       JOIN users_recovery b 
         ON b.email = a.email 
            AND b.date = a.date 
于 2013-03-20T17:57:58.753 回答
0

将 INNER JOIN 与 MAX() 一起使用

SELECT
  email,
  date
FROM users_recovery AS ur
  INNER JOIN (SELECT
        MAX(DAte) AS date
          FROM users_recovery) AS url
    ON url.date = ur.date
WHERE restore_key = '" . $code . "'
    AND complete = '0'
于 2013-03-20T18:17:45.487 回答