Mysql选择不带PHP的查询

时间:2016-06-23 20:36:16

标签: mysql sql

我编写了以下select查询,它返回了所需的记录。我知道如何在PHP中编写代码。我想知道是否有可能在一个单一的选择mysql查询

SELECT annual_end_date
    ,email
    ,expiry_id
    ,status
    ,verify
    ,email
    ,password
FROM Users
WHERE email = 'test@test.com'
    AND password = 'password'

在同一张桌子上,我还要检查(下面的伪代码PHP),返回expiry_id = 0条记录,annual_end_datedate列少then current date

If expiry_id='0' THEN
    check annual_end_date column 
    If annual_end_date is less then the current date 

如果expiry_id不等于'0',则应检查subscription table并比较pay_end_date

If expiry_id!='0' 
    check pay_end_date column in subscription table 
    If pay_end_date is less then the current date then show records

订阅表只有一个pay_end_date字段date column

1 个答案:

答案 0 :(得分:1)

您可以在查询中添加以下内容......

 AND (  ( expiry_id='0' AND annual_end_date < DATE(NOW()) )
     OR ( expiry_id<>'0' AND EXISTS ( SELECT 1
                                        FROM subscription s
                                       WHERE s.user_id = Users.user_id -- ?
                                         AND s.pay_end_date < DATE(NOW())
                                    )
        )
     )   

规范说要检查“订阅表”,但没有说明subscriptionUsers之间的关系。子查询基于对订阅中的外键列的猜测,以及Users中引用的列。