我的查询中有一个错误,但找不到原因。我想根据条件找到相同的电话号码。
我尝试了if条件。
SELECT count(t.id) as sayi from student as t
inner join student s on s.id!= t.id
where case
when one_or_two=$one_or_two and one_or_two = 1 then
s.first_phone = t.first_phone
when one_or_two=$one_or_two and one_or_two = 2 then
s.sec_phone = t.sec_phone
else 1=1
end
and t.id=4327
如果$ one_or_two = 1
此条件应正常运行s.first_phone = t.first_phone
如果$ one_or_two = 2
此条件应正常运行s.sec_phone = t.sec_phone
答案 0 :(得分:1)
您的状况
when one_or_two = $one_or_two and one_or_two = 1
等价于
one_or_two = 1
因此您的代码应减少为
SELECT count(t.id) as sayi
from student as t
inner join student s on s.id!= t.id
where case
when one_or_two = 1
then
s.first_phone = t.first_phone
when one_or_two = 2 then
s.sec_phone = t.sec_phone
else 1=1
end
and t.id=4327
或
SELECT count(t.id) as sayi
from student as t
inner join student s on s.id!= t.id
where case
when one_or_two = 1 AND s.first_phone = t.first_phone THEN 1 else 0
when one_or_two = 2 AND s.sec_phone = t.sec_phone then 1 else 0
else 0
end
and t.id=4327