SQL 中的多个 LIKE 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20806361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple LIKE statements in SQL
提问by PaulG
I have a table containing information about retail stores. I have a list of retail chain names (WalMart, Target, Eatons, etc...) When the user selects one I basically run a query to find anything having to do with that chain.
我有一张包含零售商店信息的表格。我有一个零售连锁名称列表(WalMart、Target、Eatons 等...)当用户选择一个时,我基本上会运行一个查询来查找与该连锁店有关的任何内容。
SELECT * FROM stores WHERE store_name LIKE '%$chain%' ORDER BY store_name ASC
For example, if the user selects 'WalMart', the query would return anything with the word 'WalMart' in it's name (WalMart Scarborough, WalMart Supercenter Toronto, WalMart Distribution Center etc...).
例如,如果用户选择“WalMart”,则查询将返回名称中包含“WalMart”一词的任何内容(WalMart Scarborough、WalMart Supercenter Toronto、WalMart Distribution Center 等)。
But now I would like to give the user the ability to search through this list via a search text box. The way I usually do searches is like so:
但现在我想让用户能够通过搜索文本框搜索这个列表。我通常进行搜索的方式是这样的:
SELECT * FROM stores WHERE store_name LIKE '%$user_input%' ORDER BY store_name ASC
But in this case the query will return ALL stores containing the user_input
, not just WalMarts. If I type in Toronto I would like to see WalMart Supercenter Toronto, but will of course get Target Toronto etc....
但在这种情况下,查询将返回包含 的所有商店user_input
,而不仅仅是 WalMarts。如果我在多伦多打字,我希望看到 WalMart Supercenter Toronto,但当然会得到 Target Toronto 等......
How can I make it so that I'm looking for anything containing the user_input
but also only within the WalMart subset. I would like to do this in a single query if possible. Can I use two LIKE
statements like this?
我怎样才能做到这一点,以便我正在寻找任何包含user_input
但也只包含在沃尔玛子集中的东西。如果可能,我想在单个查询中执行此操作。我可以使用两个这样的LIKE
语句吗?
Sorry, haven't tried anything yet as I'm not sure where to start.
抱歉,还没有尝试任何东西,因为我不知道从哪里开始。
回答by Hyman
Yes, you can do the following:
是的,您可以执行以下操作:
SELECT * FROM stores WHERE store_name LIKE '%$user_input%'
AND store_name LIKE '%Walmart%'
ORDER BY store_name ASC
回答by MazBros
If you want it to be limited to a single store chain you go like that:
如果您希望它仅限于单个连锁店,您可以这样做:
SELECT * FROM stores WHERE store_name LIKE '%Walmart%' AND store_name LIKE '%$user_input%' ORDER BY store_name ASC
回答by Max
Sure, you can just add two LIKE clauses:
当然,您可以添加两个 LIKE 子句:
SELECT * FROM stores
WHERE store_name LIKE '%$chain%'
AND store_name LIKE '%$user_input%'
ORDER BY store_name ASC
回答by Amir Keshavarz
This will work i think:
我认为这会起作用:
SELECT * FROM stores WHERE store_name LIKE '%$chain%' AND store_name LIKE '%$user_input%' ORDER BY store_name ASC
回答by elbuild
Yes it's possible, just use two LIKE. I would use also LOWER (applying lower case on the user_input as well) to make the query case independent.
是的,这是可能的,只需使用两个 LIKE。我也会使用 LOWER(也在 user_input 上应用小写)来使查询大小写独立。
SELECT * FROM stores WHERE LOWER(store_name) LIKE '%$user_input%'
AND LOWER(store_name) LIKE '%walmart%'
ORDER BY store_name ASC