简体   繁体   English

Sql 在将主键与 where 子句中的另一个值组合时返回空行

[英]Sql returns empty row on combinning primary key with another value in where clause

I am getting empty row on following sql我在以下 sql 中得到空行

SELECT * FROM flyers WHERE fId='6' AND 'userId'='400' 

MySQL returned an empty result set MySQL 返回一个空的结果集

(i.e. zero rows). (Query took 0.0004 sec)

But when i use但是当我使用

SELECT * FROM flyers WHERE fId='6'

Showing rows显示行

0 - 0 (1 total, Query took 0.0005 sec).

As i got my result and that is correct using only primary key.当我得到我的结果时,只使用主键是正确的。

But i does not know why mysql returns empty row on using AND with primary key.但我不知道为什么 mysql 在使用 AND 和主键时返回空行。

Note:- fId is flyer table primary key.注意:- fId 是传单表主键。

Try this:试试这个:

  1. Use back ticks for the column name not single quotes对列名使用反引号而不是单引号
  2. If userid is int data type it means you have to remove single quotes userid=400如果userid是 int 数据类型,则意味着您必须删除单引号userid=400

  3. If fid is string it means fID='6'如果fid是字符串,则表示fID='6'

    SELECT * FROM flyers WHERE fId =6 AND userId =400 SELECT * FROM flyers WHERE flyers SELECT * FROM flyers WHERE fId =6 AND userId =400

In your query you have only one mistake, which is that you used single quotes around userId .在您的查询中,您只有一个错误,即您在userId周围使用了单引号。 Use back-ticks instead or nothing:使用反引号代替或不使用:

SELECT * FROM flyers WHERE fId='6' AND userId='400'  

SELECT * FROM flyers WHERE fId=6 AND userId=400// safe not to use quotes

But I suggest not to use quotes around numbers for below reason.但我建议不要在数字周围使用引号,原因如下。

The real issue comes down to type casting.真正的问题归结为类型转换。 When you put numbers inside quotes, it is treated as a string and MySQL must convert it to a number before it can execute the query.当您将数字放在引号内时,它被视为字符串,MySQL 必须将其转换为数字才能执行查询。 While this may take a small amount of time, the real problems start to occur when MySQL doesn't do a good job of converting your string.虽然这可能需要一点时间,但当 MySQL 不能很好地转换字符串时,真正的问题就开始出现了。

For example, MySQL will convert basic strings like '123' to the integer 123, but will convert some larger numbers, like ' 18015376320243459 ', to floating point.例如,MySQL 会将基本字符串(如“123”)转换为integer 123,但会将一些较大的数字(如“ 18015376320243459 ”)转换为浮点数。 Since floating point can be rounded, your queries may return inconsistent results.由于浮点数可以四舍五入,您的查询可能会返回不一致的结果。

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

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