我想在数据库中查找重复的成员。
我一直在关注IF / ELSE声明甚至是Google中的CASE,但我似乎无法弄清楚它在我的情况下会如何起作用。
这里有一些代码在我看来是有意义的(作为一个PHP程序员),但它不是MySQL做的事情......
SELECT id FROM members WHERE (
IF (member_username = 'john.doe')
ELSEIF (member_firstname = 'John' AND member_lastname = 'Doe' AND member_email = 'john@doe.com')
ELSE (member_firstname = 'John' AND member_lastname = 'Doe')
) AND deleted = 0
如果可能的话,我如何将上述内容转换为有效的MySQL代码。
答案 0 :(得分:1)
我最终接受了@ Corbin的建议并使用了
SELECT username, first_name, last_name, email_addr FROM members WHERE username = 'username' OR (first_name = 'first name' AND last_name = 'last name')
然后使用PHP if / else语句来确定哪个记录是完全重复的。
答案 1 :(得分:0)
这会有什么问题?
SELECT id
FROM members
WHERE (member_username = 'john.doe'
OR (member_firstname = 'John' AND member_lastname = 'Doe' AND member_email = 'john@doe.com')
OR (member_firstname = 'John' AND member_lastname = 'Do'))
AND deleted = 0
答案 2 :(得分:0)
不要忘记 TOP 1!
SELECT TOP 1 id FROM members WHERE deleted = 0 AND
(
(member_username = 'john.doe') OR
(member_email = 'john@doe.com' AND member_firstname = 'John' AND member_lastname = 'Doe') OR
(member_firstname = 'John' AND member_lastname = 'Doe')
)
答案 3 :(得分:0)