有没有办法在MySQL中结合使用IN和LIKE? [英] Is there a way to combine IN and LIKE in MySQL?

查看:744
本文介绍了有没有办法在MySQL中结合使用IN和LIKE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在运行这样的查询:

I'm currently running a query like this:

SELECT *
  FROM email
 WHERE email_address LIKE 'ajones@%'
    OR email_address LIKE 'bsmith@%'
    OR email_address LIKE 'cjohnson@%'

大量的OR困扰着我.有没有一种方法可以像IN运算符那样简化它,例如:

The large number of OR's bothers me. Is there a way to condense this up with something akin to an IN operator, e.g.:

SELECT *
  FROM email 
 WHERE email_address LIKE ('ajones@%', 'bsmith@%', 'cjohnson@%')

这只是一厢情愿吗?

推荐答案

我的建议是:在@之前提取电子邮件地址的一部分,并在IN之前使用它:

Here's what I recommend: Extract the part of the email address before the @ and use that before IN:

SELECT * FROM `email`
WHERE LEFT(`email_address`, LOCATE('@', `email_address`) - 1)
        IN ('ajones', 'bsmith', 'cjohnson')

这篇关于有没有办法在MySQL中结合使用IN和LIKE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆