-1

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%";

Table

#

Query


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.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Rinku
  • 83
  • 1
  • 1
  • 13

7 Answers7

5

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:

Script Functions As Expected

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?

woodvi
  • 1,898
  • 21
  • 27
  • I found issue. I export sql file see what >> INSERT INTO `sites` (`id`, `site`) VALUES (1, '\nxyz.com\n'), (2, '\ngoogle.com\n'), (3, '\nexample.com\n'); – Rinku Nov 29 '14 at 17:13
  • 1
    Try this sql query **INSERT INTO sites (id, site) VALUES (1, '\nxyz.com\n'), (2, '\ngoogle.com\n'), (3, '\nexample.com\n');** And manually check in table. you can not find space or line break. That is a issues in sql i noticed. – Rinku Nov 30 '14 at 04:19
2

use this query

select * from sites where site = 'google.com';
keepmoving
  • 1,813
  • 8
  • 34
  • 74
1

My first answer:

Use single quotes, SELECT * FROMsitesWHEREsite= '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)
EternalHour
  • 8,308
  • 6
  • 38
  • 57
  • No there is not space or line. I have double checked. Can you please test this issue in your server. – Rinku Nov 29 '14 at 13:17
1

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';
timbmg
  • 3,192
  • 7
  • 34
  • 52
  • Not working and (attribute site into ``) does not matter – Rinku Nov 29 '14 at 12:33
  • Very strange... could you try to use the LIKE operator instead of = ? Check out here: [SQL LIKE Operator](http://www.w3schools.com/sql/sql_like.asp) – timbmg Nov 29 '14 at 12:34
1
SELECT * FROM `sites` WHERE `site` = 'google.com';
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Sharma Vikram
  • 2,440
  • 6
  • 23
  • 46
0

use this

select * from tb_name where col_name1 = 'your_value1' and col_name2 = 'your_value2' 
jay.jivani
  • 1,560
  • 1
  • 16
  • 33
  • using my query i get the data so it's working & other wise u can use `SELECT * FROM sites WHERE site LIKE 'google.com'` – jay.jivani Nov 29 '14 at 12:53
  • But try using these data. just enter id - 1 and site - google.com and then apply same query. You can point this issue. – Rinku Nov 29 '14 at 13:21
-1

Backup your data and install a fresh setup of your mysql...it might have been corrupted

Sppidy
  • 403
  • 2
  • 5
  • 15
  • I am doing this in my VPS Hosting instead localhost. – Rinku Nov 29 '14 at 12:50
  • And also tested in two other server. You can try too. – Rinku Nov 29 '14 at 13:45
  • In your case...the picture you have provided of phpmyadmin table....the sites coulumn is not showing its values normally...i guess extra spaces or enters are being inserted with google.com... Try php "TRIM" function...Insert new values with the help of TRIM function. like this – Sppidy Nov 29 '14 at 20:07
  • if this works... that means previously inserted data must have some error in it – Sppidy Nov 29 '14 at 20:13