简体   繁体   English

简单的选择查询不起作用?

[英]Simple select query not working?

PROVINCE -- varchar(25) 

SELECT * FROM listings WHERE PROVINCE='Bakersfield'

Returns empty resultset when entries with Bakersfield as province exist 当Bakersfield作为省的条目存在时,返回空结果集

I am pretty unsure as to why. 我很不确定为什么。 When I remove the WHERE it works. 当我删除WHERE它工作。

BTW I am running this in phpmyadmin, where it lets one execute SQL statements BTW我在phpmyadmin中运行它,它允许一个人执行SQL语句

In phpmyadmin it shows ... beneath the equals sign. 在phpmyadmin中,它显示在等号下面。 I am not sure why. 我不知道为什么。 That may be a hint. 这可能是一个提示。

If 如果

SELECT * FROM listings WHERE PROVINCE like '%Bakersfield%'

works for you then you have spaces in your data. 适合您,那么您的数据中就有空格。 You can remove them with 您可以删除它们

update listings
set PROVINCE = trim(PROVINCE)

See TRIM() TRIM()

After that you should check your code where you insert the data. 之后,您应该检查插入数据的代码。 Check why there are spaces inserted and fix it. 检查插入空格的原因并进行修复。

Change you equal = to LIKE operator 将等于=改为LIKE运算符

SELECT * FROM listings WHERE PROVINCE LIKE 'Bakersfield'

This should solve your issue. 这应该可以解决您的问题。 If you want to find places that includes your string simply surround it with wildcards %..% 如果你想找到包含你的字符串的地方,只需用通配符%..%环绕它

SELECT * FROM listings WHERE PROVINCE LIKE '%Bakersfield%'
SELECT * FROM listings WHERE PROVINCE='Bakersfield'

There is no point the abpve query doesnt work if the follwing case is not the problem. 如果以下情况不是问题,那么abpve查询就没有意义了。

1) If case is the prob then use upper or lower 1)如果是概率,则使用上限或下限

SELECT * FROM listings WHERE upper(PROVINCE)=upper('Bakersfield');

2) If extra space is the problem use replace or Trim 2)如果问题是额外的空间,请使用替换或修剪

SELECT * FROM listings WHERE replace(PROVINCE,' ','') ='Bakersfield'

There might be sum other seniario if not then use "Like " key word as suggested by others. 如果没有,可能会有其他的句子,然后使用其他人建议的“赞”关键词。 :) :)

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

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