0

只是想知道您是否可以从子查询中获取数据,即使主查询没有返回任何命中?

$query = "SELECT `affiliateID`, `password`, `companyName`, `contactName`,
(SELECT loginAttempts FROM lockoutRecord WHERE attemptedUsername = '$user' AND accountType = 'Affiliate' ) as attemptCount,
(SELECT lastAttemptTime FROM lockoutRecord WHERE attemptedUsername = '$user' AND     accountType = 'Affiliate' ) as lastAttemptTime
FROM `affiliates` WHERE `email` = '$user' LIMIT 1"; 

在上面的示例中,用户的登录尝试在附属表中没有找到该用户的命中,但我仍然希望它从 lockoutRecord 返回 loginAttempts 以查看对该用户名进行了多少次尝试

谢谢参观。

4

1 回答 1

1

一个可能的解决方案

SELECT affiliateID, password, companyName, contactName, loginAttempts, lastAttemptTime
FROM
(
    SELECT '$user' user 
) u LEFT JOIN affiliates a 
    ON u.user = a.email LEFT JOIN
(
    SELECT attemptedUsername, loginAttempts, lastAttemptTime
      FROM lockoutRecord 
     WHERE accountType = 'Affiliate' 
) l ON u.user = l.attemptedUsername

样本输出:

| 会员ID | 密码 | 公司名称 | 联系人姓名 | 登录尝试 | 上一次时间 |
-------------------------------------------------- ------------------------------------------
| (空) | (空) | (空) | (空) | 2 | 2013-08-09 13:00:00 |

这是SQLFiddle演示

于 2013-08-09T08:29:30.517 回答