MySQL 左连接/WHERE

发布于 2024-10-21 22:19:12 字数 955 浏览 4 评论 0原文

我将我的地址簿加入到我的电子邮件表中,如下所示:

SELECT * FROM messages m
LEFT JOIN addr_book a ON 
m.to_msg LIKE a.h_email 
OR m.to_msg LIKE a.bill_email 
OR m.to_msg LIKE a.b_email 
OR m.to_msg LIKE a.w_email 
OR m.to_msg LIKE a.other_email 
OR m.from_msg LIKE a.h_email 
OR m.from_msg LIKE a.bill_email 
OR m.from_msg LIKE a.b_email 
OR m.from_msg LIKE a.w_email 
OR m.from_msg LIKE a.other_email 
OR m.cc_msg LIKE a.h_email 
OR m.cc_msg LIKE a.bill_email 
OR m.cc_msg LIKE a.b_email 
OR m.cc_msg LIKE a.w_email 
OR m.cc_msg LIKE a.other_email 
OR m.bcc_msg LIKE a.h_email 
OR m.bcc_msg LIKE a.bill_email 
OR m.bcc_msg LIKE a.b_email 
OR m.bcc_msg LIKE a.w_email 
OR m.bcc_msg LIKE a.other_email

是它会获取所有内容,因为某些联系人没有电子邮件,并且会加入没有 cc_msg='' 或 bcc_msg='' 的电子邮件

问题 我可以在列上左连接一个表,其中该列类似于“@”?

我尝试了一些诸如 IN (m.to_message LIKE a.h_email WHERE a.h_email LIKE '%@%') OR

但我不断收到错误。

有什么想法吗?

非常感谢!

I am joining my address book onto my email table like this:

SELECT * FROM messages m
LEFT JOIN addr_book a ON 
m.to_msg LIKE a.h_email 
OR m.to_msg LIKE a.bill_email 
OR m.to_msg LIKE a.b_email 
OR m.to_msg LIKE a.w_email 
OR m.to_msg LIKE a.other_email 
OR m.from_msg LIKE a.h_email 
OR m.from_msg LIKE a.bill_email 
OR m.from_msg LIKE a.b_email 
OR m.from_msg LIKE a.w_email 
OR m.from_msg LIKE a.other_email 
OR m.cc_msg LIKE a.h_email 
OR m.cc_msg LIKE a.bill_email 
OR m.cc_msg LIKE a.b_email 
OR m.cc_msg LIKE a.w_email 
OR m.cc_msg LIKE a.other_email 
OR m.bcc_msg LIKE a.h_email 
OR m.bcc_msg LIKE a.bill_email 
OR m.bcc_msg LIKE a.b_email 
OR m.bcc_msg LIKE a.w_email 
OR m.bcc_msg LIKE a.other_email

The problem is it fetches everything, because some contacts don't have emails and join up with emails that don't have cc_msg='' or bcc_msg=''

Is there a way I can left join a table on a column where the column is like '@'??

I tried a couple things like IN (m.to_message LIKE a.h_email WHERE a.h_email LIKE '%@%') OR etc etc

But I keep getting errors.

Any ideas?

Thanks a ton!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烙印 2024-10-28 22:19:12

如果我理解你的问题,你需要使用 AND 而不是 WHERE:

ON (m.to_message LIKE a.h_email AND a.h_email LIKE '%@%') OR (...

我认为你可能意味着 = 而不是 LIKE 对于第一个测试,这会给出这个:

ON (m.to_message = a.h_email AND a.h_email LIKE '%@%') OR (...

或者也许你只需要这个:

ON m.to_message = a.h_email

与 LIKE 不同,如果 a.h_email 为空但 m.to_message 非空,则相等性测试将失败。

If I understand your question, you need to use AND instead of WHERE:

ON (m.to_message LIKE a.h_email AND a.h_email LIKE '%@%') OR (...

I think you might mean = instead of LIKE for the first test, which would give this:

ON (m.to_message = a.h_email AND a.h_email LIKE '%@%') OR (...

Or perhaps you just need this:

ON m.to_message = a.h_email

Unlike LIKE, an equality test will fail if a.h_email is empty but m.to_message is non-empty.

音栖息无 2024-10-28 22:19:12

好的,这很有帮助。我不知道为什么,但我一直假设 = 在 mySQL 中区分大小写。我使用了以下内容,效果非常好。谢谢大家,我确信如果我接受过适当的培训,我一开始就不会犯这个错误,哈哈。

LEFT JOIN addr_book a ON 

(m.to_msg=a.h_email OR m.to_msg=a.bill_email OR m.to_msg=a.b_email OR m.to_msg=a.w_email OR m.to_msg=a.other_email) OR

(m.from_msg=a.h_email OR m.from_msg=a.bill_email OR m.from_msg=a.b_email OR m.from_msg=a.w_email OR m.from_msg=a.other_email) OR

((m.cc_msg!='') AND (m.cc_msg=a.h_email OR m.cc_msg=a.bill_email OR m.cc_msg=a.b_email OR m.cc_msg=a.w_email OR m.cc_msg=a.other_email)) OR 

((m.bcc_msg!='') AND (m.bcc_msg=a.h_email OR m.bcc_msg=a.bill_email OR m.bcc_msg=a.b_email OR m.bcc_msg=a.w_email OR m.bcc_msg=a.other_email))

Ok so that helps out alot. I don't know why, but I have always assumed = is case sensitive in mySQL. I used the following and it works beautifully. Thanks all, I'm sure if I had proper training I wouldn't have made this mistake in the first place lol.

LEFT JOIN addr_book a ON 

(m.to_msg=a.h_email OR m.to_msg=a.bill_email OR m.to_msg=a.b_email OR m.to_msg=a.w_email OR m.to_msg=a.other_email) OR

(m.from_msg=a.h_email OR m.from_msg=a.bill_email OR m.from_msg=a.b_email OR m.from_msg=a.w_email OR m.from_msg=a.other_email) OR

((m.cc_msg!='') AND (m.cc_msg=a.h_email OR m.cc_msg=a.bill_email OR m.cc_msg=a.b_email OR m.cc_msg=a.w_email OR m.cc_msg=a.other_email)) OR 

((m.bcc_msg!='') AND (m.bcc_msg=a.h_email OR m.bcc_msg=a.bill_email OR m.bcc_msg=a.b_email OR m.bcc_msg=a.w_email OR m.bcc_msg=a.other_email))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文