我在mysql的多列中遇到匹配/搜索值的问题。
在我的表中,我有姓名,所有者,电子邮件,altemail,altemail2,altemail3列名。
name = John; email = john@doe.com
现在名称可能在名称或所有者列中,电子邮件可能在以上四列中。
现在我想进行搜索并列出名称和电子邮件都存在的结果。意味着名称和电子邮件不仅应与姓名或电子邮件相匹配。
这是我到目前为止所做的,但没有奏效:
SELECT*
FROM $table
WHERE name = '%s' OR owner = '%s' AND email = '%s' OR alt_email = '%s'
OR alt_email2 = '%s' OR alt_email3 = '%s'",$name,$email,$email,$email,$email);
但是如果名称匹配,则显示结果并忽略电子邮件,或者如果电子邮件匹配,则显示结果将忽略该名称。但我需要用户信息来匹配名称和电子邮件。
再次感谢您的帮助。
答案 0 :(得分:1)
将您的查询更改为此
SELECT*
FROM $table
WHERE (name = '%s' OR owner = '%s') AND (email = '%s' OR alt_email = '%s'
OR alt_email2 = '%s' OR alt_email3 = '%s')",$name,$name,$email,$email,$email,$email);
答案 1 :(得分:0)
这应该有效:
SELECT * FROM $table WHERE (name = '%s' OR owner = '%s') AND (email = '%s' OR alt_email = '%s' OR alt_email2 = '%s' OR alt_email3 = '%s')",$name,$email,$email,$email,$email);