如何确定WHERE子句中的哪个条件失败? [英] How can I determine which condition on WHERE clause fails?
本文介绍了如何确定WHERE子句中的哪个条件失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的表:
// cookies
+----+---------+-------------------+------------+
| id | user_id | token | expire |
+----+---------+-------------------+------------+
| 1 | 32423 | dki3j4rf9u3e40... | 1467586386 |
| 2 | 65734 | erhj5473fv34gv... | 1467586521 |
| 3 | 21432 | 8u34ijf34t43gf... | 1467586640 |
+----+---------+-------------------+------------+
我有这个查询:
SELECT 1
FROM cookies
WHERE user_id = :id AND
token = :token
注意:结果始终为 one 行或 zero 行. (令牌列为unique
)
Note: Always the result is either one row or zero row. (token column is unique
)
当有匹配的行时,很好,但是,当没有匹配的行时,我想了解为什么?
When there is a matched row, ok, all fine, but when there isn't any matched row, I want to understand why?
-
user_id
存在,但token
不存在 -
token
存在,但user_id
不存在 - 所有这些列都不存在
user_id
exists buttoken
doesn'ttoken
exists butuser_id
doesn't- non of those columns doesn't exist
如何确定未选择任何行(匹配)"的原因?
编辑:这是所有可能的输出:
Here is all possible outputs:
- 行存在
- 行不存在,因为
user_id = :id
为 false - 行不存在,因为
token = :token
为 false - 行不存在,因为
user_id = :id
和token = :token
均为 false - 行不存在,因为
user_id = :id
和token = :token
是 true ,但不在同一行中.
- row exists
- row doesn't exist because
user_id = :id
is false - row doesn't exist because
token = :token
is false - row doesn't exist because both
user_id = :id
andtoken = :token
are false - row doesn't exist because
user_id = :id
andtoken = :token
are true but not in the same row.
推荐答案
这总是返回一行.如果匹配,则为1表示匹配,否则为0.它还显示cookie表中是否存在userID或令牌.使用此方法,您可以确定where子句失败的原因.
This always returns one row. 1 for matched if there is a match otherwise 0. It also shows if the userID or the token exists in the cookies table. Using this method you can determine why the where clause failed.
SELECT ( select count(*) > 0 matched from cookies where user_id = :id and token = :token ) matched
, ( select count(*) > 0 userIdExists from cookies where user_id = :id ) userIdExists
, ( select count(*) > 0 tokenExists from cookies where token = :token ) tokenExists
这篇关于如何确定WHERE子句中的哪个条件失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文