简体   繁体   English

SQL LIKE + 通配符只返回第一个值的结果

[英]SQL LIKE + wildcard operator only returns results with first value

What's happening StackOverflow. StackOverflow 发生了什么。

I'm using DB Browser for SQLLite to query a database with two tables - people and states .我正在使用DB Browser for SQLLite来查询具有两个表的数据库 - peoplestates people has the field state_code and states has the fields state_abbrev and state_name . people有字段state_codestates有字段state_abbrevstate_name people.state_code and states.state_abbrev both have postal codes for USA states (eg AK, AZ, MI, MN, etc.). people.state_codestates.state_abbrev都有美国各州的邮政编码(例如 AK、AZ、MI、MN 等)。

I'm trying to search for all records where the state names match a certain text string criteria, using LIKE and wildcards.我正在尝试使用 LIKE 和通配符搜索 state 名称与特定文本字符串条件匹配的所有记录。 Here's my code:这是我的代码:

SELECT *
FROM people
WHERE state_code = (
    SELECT state_abbrev FROM states WHERE state_name LIKE 'mi%');

I want the above code to return all records where the state name begins with "mi" - ie Michigan, Minnesota, and Missouri.我希望上面的代码返回 state 名称以“mi”开头的所有记录 - 即密歇根州、明尼苏达州和密苏里州。 However, this is only returning records with the first alphabetical state name - Michigan .但是,这只会返回第一个字母顺序为 state 名称 - Michigan的记录。 Same happens with LIKE '%ans%' - this only returns records from Ark ans as, despite the existence of records from K ans as. LIKE '%ans%'发生同样的情况——这只返回来自 Ark ans as 的记录,尽管存在来自 K ans as 的记录。

What am I doing wrong yall?我做错了什么? I've tried using GROUP BY state_name, state_abbrev within the nested SELECT to no avail, and I can't seem to find anyone else encountering the same issue.我尝试在嵌套的SELECT中使用GROUP BY state_name, state_abbrev无济于事,而且我似乎找不到其他人遇到同样的问题。

You can try using WHERE state_code IN (... this will search for all the records in your inner sql query您可以尝试使用WHERE state_code IN (...这将搜索您内部 sql 查询中的所有记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM