tags:

views:

128

answers:

7

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:

| 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.

What am I doing wrong?

+1  A: 

Throw away apostrophes around field name, You may use them but apostrophes around field names are opposite = "´"

SELECT * FROM tableA WHERE fieldD='A LONG STRING WITH SYMBOLS AND CHARCTERS'
Rafal Ziolkowski
tried that and still not getting any results. Here is the query from PhPMyAdminSELECT * FROM table WHERE fieldD="100/80R16 M A39 50S"
Ole Media
Do not use <code>"</code> for constants. Your constant is now a field name. Try <code>'</code>: ... where fieldD = '100/80R16 M A39 50S'
serge_bg
A: 

Not sure if this was a typo in your query above, but fieldD should not be in single quotes.

Matt Wrock
+5  A: 

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. 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';
tj111
tried that and still not getting any results. Here is the query from PhPMyAdminSELECT * FROM table WHERE fieldD="100/80R16 M A39 50S"
Ole Media
+1: MySQL uses back-quotes to indicate field names - not straight single quotes. So I think your basic diagnosis is correct.
Jonathan Leffler
So what am I doing wrong? why I'm getting 0 results when it exists? trying from PhPMyAdmin and I know that exist the record, don't understand why. Thinking out loud, could it be I'm missing the Primary Key?
Ole Media
+1  A: 

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

Also, I noticed a typo on the word 'CHARACTERS'.

randy melder
A: 

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

Phill Pafford
A: 

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

I had to place a %' at the end of the string to work

Ole Media
That probably means that your field has some unprintable characters at the end. If you try "SELECT HEX(SUBSTRING(myfield,-1) FROM mytable" you'll be able to see if it is something unexpected.
dnagirl
A: 

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.

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.

longneck
When trying what you say above, for the first query I get 1 result, for the second query I get the 90,000 results. Someone here posted that this could be because of unprintable characters at the end, which may make sense because I import the data from a CSV file the client provided. How do I get rid of these characters?
Ole Media
right, that's why you need to find the appropriate row and show me what the hex() value is. then we can figure out what the unprintable character is.
longneck
Find out the problem was that all my records had an invisible blank space, this is why I was not getting any results. Thanks for trying to help me out. I created a php script using the trim() function and update all my records, and this solved everything.
Ole Media