在 sql 中找到导致 return 的 where 子句部分
find the part of the where clause that cause return in sql
我有一个 mysql 查询是这样的,
SELECT USER
FROM users
WHERE user_name = someUserName
OR location = someLocation
OR deleted = 1
LIMIT 1
我试图弄清楚是哪一种情况导致用户被选中。这可能吗?
通过选择过滤列,您可以找到有效的列。
SELECT USER,
user_name,
location,
deleted,
CASE
WHEN user_name = someUserName
AND location <> someLocation
AND deleted <> 1 THEN 'User_name'
WHEN location = someLocation
AND user_name <> someUserName
AND deleted <> 1 THEN 'location'
WHEN location <> someLocation
AND user_name <> someUserName
AND deleted = 1 THEN 'deleted'
WHEN user_name = someUserName
AND location = someLocation
AND deleted <> 1 THEN 'User_name,location'
WHEN user_name = someUserName
AND location <> someLocation
AND deleted = 1 THEN 'User_name,deleted'
WHEN user_name <> someUserName
AND location = someLocation
AND deleted = 1 THEN 'location,deleted'
WHEN user_name = someUserName
AND location = someLocation
AND deleted = 1 THEN 'ALL'
END AS Filer_Column
FROM users
WHERE user_name = someUserName
OR location = someLocation
OR deleted = 1
LIMIT 1
另外 Limit
没有 order by
对我来说没有任何意义。最好加上要限制结果集的顺序
如果你想指出满足所有条件(如果不止一个),你可以这样解决问题:
select user, group_concat(condition_met) as conditions_met
from (select user, 'A' as condition_met
from users
where user_name = someusername
union all
select user, 'B'
from users
where location = someLocation
union all
select user, 'C'
from users
where deleted = 1) x
group by user
这在功能上应该是等价的:
select user,
sum(user_name = someusername) as cond_a,
sum(location = someLocation) as cond_b,
sum(deleted = 1)) as as cond_c
from users
where user_name = someusername
or location = somelocation
or deleted = 1
group by user
有时我正在调试一些东西,我希望能够回答同样类型的问题。显然,您可以注释掉部分查询并 运行 那样。如果您不确定表达式或空值,或者您只是想确认您的理智,那么您可以尝试使用 case 表达式。
SELECT
user
case when user_name = someUserName then 'true' else 'false' end as Condition1,
case when location = someLocation then 'true' else 'false' end as Condition2,
case when deleted = 1 then 'true' else 'false' end as Condition 3
FROM users
WHERE
user_name = someUserName
OR location = someLocation
OR deleted = 1
LIMIT 1
我有一个 mysql 查询是这样的,
SELECT USER
FROM users
WHERE user_name = someUserName
OR location = someLocation
OR deleted = 1
LIMIT 1
我试图弄清楚是哪一种情况导致用户被选中。这可能吗?
通过选择过滤列,您可以找到有效的列。
SELECT USER,
user_name,
location,
deleted,
CASE
WHEN user_name = someUserName
AND location <> someLocation
AND deleted <> 1 THEN 'User_name'
WHEN location = someLocation
AND user_name <> someUserName
AND deleted <> 1 THEN 'location'
WHEN location <> someLocation
AND user_name <> someUserName
AND deleted = 1 THEN 'deleted'
WHEN user_name = someUserName
AND location = someLocation
AND deleted <> 1 THEN 'User_name,location'
WHEN user_name = someUserName
AND location <> someLocation
AND deleted = 1 THEN 'User_name,deleted'
WHEN user_name <> someUserName
AND location = someLocation
AND deleted = 1 THEN 'location,deleted'
WHEN user_name = someUserName
AND location = someLocation
AND deleted = 1 THEN 'ALL'
END AS Filer_Column
FROM users
WHERE user_name = someUserName
OR location = someLocation
OR deleted = 1
LIMIT 1
另外 Limit
没有 order by
对我来说没有任何意义。最好加上要限制结果集的顺序
如果你想指出满足所有条件(如果不止一个),你可以这样解决问题:
select user, group_concat(condition_met) as conditions_met
from (select user, 'A' as condition_met
from users
where user_name = someusername
union all
select user, 'B'
from users
where location = someLocation
union all
select user, 'C'
from users
where deleted = 1) x
group by user
这在功能上应该是等价的:
select user,
sum(user_name = someusername) as cond_a,
sum(location = someLocation) as cond_b,
sum(deleted = 1)) as as cond_c
from users
where user_name = someusername
or location = somelocation
or deleted = 1
group by user
有时我正在调试一些东西,我希望能够回答同样类型的问题。显然,您可以注释掉部分查询并 运行 那样。如果您不确定表达式或空值,或者您只是想确认您的理智,那么您可以尝试使用 case 表达式。
SELECT
user
case when user_name = someUserName then 'true' else 'false' end as Condition1,
case when location = someLocation then 'true' else 'false' end as Condition2,
case when deleted = 1 then 'true' else 'false' end as Condition 3
FROM users
WHERE
user_name = someUserName
OR location = someLocation
OR deleted = 1
LIMIT 1