简体   繁体   English

为什么MySQL对此查询始终不返回任何数据?

[英]Why does MySQL always return no data for this query?

I have a database in which a few fields are set as index. 我有一个数据库,其中一些字段设置为索引。 I don't know if this has to do with the problem or not, but when creating a query I get no results. 我不知道这是否与问题有关,但是在创建查询时却没有结果。 Here is how I have things set in the database. 这是我在数据库中设置的方式。 The database has 90,000 records and this table is set like: 该数据库有90,000条记录,该表的设置如下:

| fieldA  | fieldB | fieldC  | fieldD |

fieldA = index - CHAR
fieldB = index - CHAR
fieldC = index - CHAR
fieldD = index - VARCHAR

If I do: 如果我做:

SELECT * 
  FROM tableA 
 WHERE 'fieldD'='A LONG STRING WITH SYMBOLS AND CHARCTERS';

I get 0 results and I know that the value for fieldD exist and is there. 我得到0个结果,并且我知道fieldD的值存在并且在那里。

What am I doing wrong? 我究竟做错了什么?

It looks to me like your problem is you're enclosing your column names with single quotes. 在我看来,您的问题是您用单引号将列名引起来。 This is causing MySQL to treat it as a string instead of a column name, so you are literally comparing the string 'fieldA' with long string. 这导致MySQL将其视为字符串而不是列名,因此您实际上是在将字符串“ fieldA”与长字符串进行比较。 Remove the quotes from around the column name and you should be good to go. 删除列名周围的引号,您应该一切顺利。

SELECT * FROM tableA WHERE fieldD='A LONG STRING WITH SYMBOLS AND CHARCTERS';

扔掉字段名称周围的撇号,您可以使用它们,但字段名称周围的撇号相反=“´”

SELECT * FROM tableA WHERE fieldD='A LONG STRING WITH SYMBOLS AND CHARCTERS'

There could be a problem with character encoding here. 此处的字符编码可能存在问题。 I would try this query and see what happens: SELECT * FROM tableA WHERE 'fieldD' like '%A LONG STRING WITH SYMBOLS AND CHARCTERS%'; 我将尝试执行此查询,看看会发生什么:SELECT * FROM tableA在“ fieldD”(如“带有符号和字符的%A长字符串%”)中;

Also, I noticed a typo on the word 'CHARACTERS'. 另外,我注意到“ CHARACTERS”一词有错字。

不知道这是否是上面查询中的错字,但fieldD不应使用单引号引起来。

BACKTICKS instead of single quotes 反引号而不是单引号

SELECT * FROM tableA WHERE `fieldD`='A LONG STRING WITH SYMBOLS AND CHARCTERS';

The backticks key is to the left of the 1 key 反引号键位于1键的左侧

I don't know why but the following query did the trick. 我不知道为什么,但是以下查询成功了。 If any of you can explain why will be appreciated: 如果有人可以解释为什么,将不胜感激:

SELECT * FROM tableA WHERE completo LIKE ( '1008017 P MT/75 FRONT 52H%' ) LIMIT 0 , 30 SELECT * FROM tableA WHERE COMPLETO LIKE( '1008017 P MT / 75 FRONT 52H%')LIMIT 0,30

I had to place a %' at the end of the string to work 我必须在字符串的末尾放置%'才能工作

the column does not have the data in it you think it does. 该列中没有您认为有的数据。 try this: 尝试这个:

first run this query: 首先运行此查询:

select hex('100/80R16 M A39 50S')

then run this query: 然后运行以下查询:

select fieldD, hex(fieldD) from tableA

then compare the results from the first query to the appropriate row from the second query. 然后将第一个查询的结果与第二个查询的相应行进行比较。 i think you will find the hex() representations do not match. 我认为您会发现hex()表示形式不匹配。

if you can't tell why they don't match from the hex() output, post it here and i'll help you figure out why it's not working. 如果您无法从hex()输出中找出它们为什么不匹配的原因,请将其发布在此处,我会帮助您弄清楚为什么它不起作用。

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

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