SQL where子句在我的数据库中不起作用。
我有一个名为“站点”的表,其结构如下
id site
1 xyz.com
2 google.com
3 example.com
我正在运行此SQL查询
SELECT * FROM `sites` WHERE `site` = "google.com";
但是我得到这个输出
MySQL returned an empty result set (i.e. zero rows). (Query took 0.0009 sec)
我一生中从未见过这样的事。
更新:屏幕截图
我不想在项目中应用此查询。
SELECT * FROM `sites` WHERE `site` LIKE "%google.com%";
#
真正的问题出insert
在创建数据库的命令中。尝试
INSERT INTO sites (id, site) VALUES (1, '\nxyz.com\n'), (2, '\ngoogle.com\n'), (3, '\nexample.com\n')
并手动检查表中的记录。您不会看到换行符。我注意到这是SQL中的一个问题。
UPDATE: OP had invisible newline characters (\n) in his dataset. @EternalPoster (and I) supposed that Trim would remove all whitespace, but MySql Trim Documentation specifies leading & trailing spaces only.
This is what I did:
-- for http://stackoverflow.com/questions/27203169/sql-query-not-work-for-google-com
-- and http://stackoverflow.com/questions/27202157/sql-where-clause-not-working
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
-- --------------------------------------------------------
DROP TABLE IF EXISTS `sites`;
--
-- structure for table `sites`
--
CREATE TABLE IF NOT EXISTS `sites` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`site` varchar(32) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
--
-- data for table `sites`
--
INSERT INTO `sites` (`id`, `site`) VALUES
(1, 'xyz.com'),
(2, 'google.com'),
(3, 'example.com');
--
-- select google
--
SELECT *
FROM sites
WHERE site = 'google.com'
;
--
-- select google
--
SELECT *
FROM sites
WHERE site = 'google.com'
;
and this is what I got:
So in my case, I see the script functioning as expected.
您的案子有什么不同?我的安装是一个相当默认的设置。Like '%google.com%'
适用于您的数据集的事实说明了两点。人们已经建议使用TRIM
,因为Like表达式将匹配不可见的字符(空格,制表符,退格键,空值)。MySQL对于正则表达式有一个单独的运算符REGEXP,因此似乎不是该.
字符用作通配符,但这可能值得一看。
创建一个空的数据库,然后尝试运行上面的脚本。您得到与我相同的结果吗?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句