问题
SQL where clause is not working in my database.
I have a table called "sites" and structure like that
id site
1 xyz.com
2 google.com
3 example.com
I am running this SQL query
SELECT * FROM `sites` WHERE `site` = "google.com";
But I am getting this output
MySQL returned an empty result set (i.e. zero rows). (Query took 0.0009 sec)
I never see before like that in my life.
Update: Screenshot
I do not want to apply this query in project.
SELECT * FROM `sites` WHERE `site` LIKE "%google.com%";
#
The real problem was in insert
commands on creation of DB.
Try
INSERT INTO sites (id, site) VALUES (1, '\nxyz.com\n'), (2, '\ngoogle.com\n'), (3, '\nexample.com\n')
and manually check records in the table. You would not see line breaks. This is an issue in SQL I've noticed.
回答1:
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.
What's different about your case?
My installation is a fairly default setup.
The fact that Like '%google.com%'
works on your dataset suggests a couple things. Folks have already suggested TRIM
, because the Like expression would match invisible characters (spaces, tabs, backspaces, nulls). MySQL has a separate operator REGEXP for regular expressions, so it wouldn't seem to be that the .
character is being used as a wildcard, but that might be worth a look.
Create an empty database and try running my script above. Do you get the same result I do?
回答2:
use this query
select * from sites where site = 'google.com';
回答3:
You have to use singe quotes as described in the mysql manual. Have a look at the last example.
Besides, you should get rid of the `` around site
SELECT *
FROM `sites`
WHERE site = 'google.com';
回答4:
SELECT * FROM `sites` WHERE `site` = 'google.com';
回答5:
My first answer:
Use single quotes, SELECT * FROM
sitesWHERE
site= 'google.com';
In regards to the single/double quote syntax, it depends on your SQL Mode. From the MySQL documentation:
The ANSI_QUOTES mode causes the server to interpret double-quoted strings as identifiers. Consequently, when this mode is enabled, string literals must be enclosed within single quotation marks. They cannot be enclosed within double quotation marks. The server SQL mode is controlled as described in Section 5.1.6, “Server SQL Modes”.
My second answer after OP's edit:
After seeing the screenshots, it looks like you have a blank line above the URL's in your database or whitespace. You'll need to remove it manually or with TRIM.
To test if there is whitespace, you can use a wildcard:
SELECT `site` FROM `sites` WHERE `site` LIKE '%google.com%'
If you get a result you know there's whitespace. if you want to workaround the issue without permanently removing the whitespace:
SELECT TRIM(site) FROM `sites` WHERE `site` LIKE '%google.com%'
To permanently remove the whitespace from only one row:
UPDATE `sites` SET `site` = TRIM(site) WHERE `site` LIKE '%google.com%'
To permanently remove the whitespace from all rows (backup table first), you can do:
UPDATE `sites` SET `site` = TRIM(site)
回答6:
use this
select * from tb_name where col_name1 = 'your_value1' and col_name2 = 'your_value2'
回答7:
Backup your data and install a fresh setup of your mysql...it might have been corrupted
来源:https://stackoverflow.com/questions/27202157/sql-where-clause-not-working