Why does a MySQL not recognize a table value? - mysql

Friends
For some reason, a MySQL query refuses to recognize a particular value in a table.
The table contains columns
"idLookUp", "LUgroup", "LUvalue"
212 adispo AdmICU_Interv
213 adispo AdmICU_noInterv
SELECT * FROM LookUp WHERE LUvalue = "AdmICU_Interv";
returns no records!
SELECT * FROM LookUp WHERE LUvalue = "AdmICU_noInterv";
returns the proper record (#212).
SELECT * FROM LookUp WHERE idLookUp = 212
returns the proper record
The string AdmICU_Interv doesn't figure anywhere else and is not a reserved phrase (from what I can tell). I'm sure I'm missing something stupid here, but I can't figure out what is causing this behavior.
I'd appreciate any hints. Thx!
jon

could you have some hidden char try using trim()
SELECT * FROM LookUp WHERE trim(LUvalue) = "AdmICU_Interv";
or like
SELECT * FROM LookUp WHERE LUvalue like "%AdmICU_Interv%";

and this:
SELECT * FROM LookUp WHERE LUvalue LIKE "%AdmICU_Interv%"

Thanks Folks, very helpful. Yes, there was something in there I could not see. Still don't know what. Length was 13 when it should have been 12. Overwriting with the correct string solved the problem.
Since this was a lookup table, I have to go back and update all records with the problematic entry, but MySQL does that easily:
UPDATE LookUp SET LUvalue = 'AdmICU_Interv' WHERE LUvalue LIKE "%AdmICU_Interv%";
I do appreciate the problem solving hints. Hope this helps somebody else.
Cheers
Jon

Related

MySQL return no results from select * where varchar="" query

I'm having issue with my study project for creating database in MySQL.
I've imported data using LOAD to my created table from a CSV file. Now when I'm executing select * from mytable everything show up perfectly, but when I execute select * from bi_jogging.routes as r where r.Creator_Email="jhenderson7c#bbb.org"
I get nothing.
The email is there, I've tried different syntax options but it seems to be something else, I suspect something with the varchar format, I really have nothing in mind.
For other tables it works fine and others not.
You can try using the query:
select * from bi_jogging.routes as r where r.Creator_Email like "%jhenderson7c#bbb.org%"
If like operator shows the result then there may be white spaces in the email, please double check..
For join try this:
select * from bi_jogging.routes as r join bi_jogging.buddies as b
on b.Email like '%r.Creator_Email%'
I think it should work. Again check with same code.
select * from bi_jogging.routes as r where r.Creator_Email='jhenderson7c#bbb.org'
if [select * from mytable] this works ,then try to copy the email from result and paste it in where clause.
There may be conflicts between quotes.
your table entry contains quotes???
check properly. i think you have quotes in your table entry,so when you try this,
select * from bi_jogging.routes as r where r.Creator_Email like "%jhenderson7c#bbb.org%"
'%' sign matches with any character and you will get the result.
Inside the tablejhenderson7c#bbb.org and "jhenderson7c#bbb.org" are completely different.
I found spaces in the mysql tables after few emails, so I guess that was it. burned 8 hours on this one, thank you all. I could not find the spaces at the end of the mail by looking at it, I had to hit backspace to see that only after two hits the last char is deleted
this helped me : UPDATE bi_jogging.results set Mail_Found = TRIM(Replace(Replace(Replace(Mail_Found,'\t',''),'\n',''),'\r',''));

SELECT * FROM games WHERE

I am having issues with my MySQL syntax. I would like to run a select query where either one of two options are true. However the following code does not work.
SELECT * FROM games WHERE genre="indie" OR title="indie"
I have been fooling around and look at other threads and have found out how to use OR to check the same column for multiple entries but not a way to check different columns for the same entries. When I do:
SELECT * FROM games WHERE genre="indie"
The query works fine. Any help would be greatly appreciated.
The only way I see this really would't work, is if you've mistyped the name of the column 'title' (if the second query you wrote works)
The assumptions about the case sensitivity are wrong, since the second query returns something, the first should return at least the same rows as the second one
In MySQL " " works just as ' ', so this assuption was wrong too.
If you post more information, it would be easier to help you
Maybe you ignoring the upper/lower case? Also use like
You can use this:
SELECT * FROM games WHERE (LOWER(genre) like 'indie') OR (LOWER(title) like 'indie')

Escaping % sign in subquery

I have a query in mySQL that's meant to return search terms that are used on our site. Yes, this is for a tag cloud, and yes, I know it's a mullet :)
We've got an admin page where administrators can view search terms and choose to exclude them from showing up in the cloud. These words go into the "badWords" table. We've gotten some terms like "foo%2525252525252520bar", and we're having trouble getting those excluded.
In pseudocode, the query to get the search terms for the cloud is:
SELECT * FROM `searchTerms` WHERE `word` NOT IN ( SELECT `word` FROM `badWords` )
This works fine, unless one of the terms returned from the subquery has a % in it. Is there a way to escape the entire subquery? I've tried doing a
replace( SELECT `word` FROM `badWords`, '%', '\%' )
... but that's apparently not syntactically correct.
I can do two queries if need be, but wondered if there's a way to get it done as is.
Thanks!
==============================
UPDATE: closing this for now, as I think the error lies elsewhere. Will report back once I know for sure, but don't want folks wasting time answering the question here if it's not the correct question...
Upvoted both of the replies received so far. Thanks, guys.
==============================
UPDATE 2: sigh Nevermind... can't close it :\
==============================
FINAL UPDATE: Well, looks like escaping the value isn't the problem. The admin page passes the value in the URL before it's added to the badWords table. In passing the value via the URL, it changes. So what's added to badWords is actually "foo%25252525252520bar" (there's one less "25" sequence). If I manually update the value in badWords and add back the missing "25" it works as expected. So no need to replace or escape anything. I just need to fix those URL values properly.
==============================
I don't think the % is your problem here. I think that you're trying to use REPLACE() on the subquery itself (SELECT ...), and not on a column value (word). Try this instead:
SELECT * FROM `searchTerms`
WHERE `word` NOT IN (
SELECT REPLACE(`word`, '%', '\%') AS word FROM `badWords`
);
Good luck!
I'm not very good with MySQL syntax, but SQL Server let's you do it this way:
SELECT * FROM `searchTerms` WHERE `word` NOT IN ( SELECT REPLACE(`word`, '%', '\%') FROM `badWords` )
NOTE: Basically all I did was move your REPLACE over some =) Hope this helps.

mysql: selecting rows if a number is in a list field (ie. userIDs = "1,2,3", select * from table where 1 in userIDs)

i store lists of ids inside fields (by list i mean val1,val2,val3,...), and i want to select rows from a table where the field has valX in the list.
i thought this would work, but apparently not :(
SELECT *
FROM userbulletins
WHERE 25 IN `currentMessages`
OR 25 IN `removedMessages`
OR 25 IN `readMessages`
currentMessages, removedMessages, and readMessages are fields with ids stored as lists
i've also tried
SELECT *
FROM userbulletins
WHERE "25" IN(`currentMessages`)
both return nothing (and should return 1 row in my test db)
any idea what i'm doing wrong? or if this is even possible?
thanks!
If I understand correctly, you have a denormalized table where the currentMessages value can be "val1,val2,val3,..." and you want to find rows where 25 is one of those values? If so, use the FIND_IN_SET function:
WHERE FIND_IN_SET('25', `currentMessages`) > 0
OR FIND_IN_SET('25', `removedMessages`) > 0
OR FIND_IN_SET('25', `readMessages`) > 0
...but I really recommend normalizing your data to make querying easier.
You can use FIND_IN_SET for this purpose:
SELECT *
FROM userbulletins
WHERE FIND_IN_SET("25", `currentMessages`)
It would also be worth considering if a different design would be better where you don't have a list of values in a single cell. I recommend that you read this Wikipedia article on first normal form.
That's because IN doesn't work like that. In is for a stated list of values or a list resulting from a subquery.
Either you have to have currentMessages expressed as a string, in which case WHERE currentMessages LIKE '%|25|%' OR currentMessage LIKE '25|%' OR currentMessage LIKE '%|25' OR currentMessage = '25' will work messily, or you have to have another table (or a few of them), such that it's
SELECT * FROM userbulletins
WHERE 25 IN (SELECT * FROM `currentMessages` WHERE....)
--In that case, I'd advise messages(id,[title,content,whatever],status{current,removed,read})
fakeEDIT: The solutions with FIND_IN_SET are better than my LIKE option, but I would still suggest using a table for it.

SHA salt in MySQL query

I am setting a cookie. Something like:
$_COOKIE['test'] = SHA1('124'.'mysalt');
Now 124 is my id which I want. So in my MySQL table, I am trying to run a query like:
$sql = ("SELECT * FROM users WHERE SHA1(`id`) = '".mysql_real_escape_string($_COOKIE['test'])."'");
How to add the "mysalt" to the SQL query? Because else I want get the correct id.
Use can use Concat() for that.
SELECT ... Sha1( Concat(`id`, 'mysalt') )=...
The query should be:
$sql = ("SELECT * FROM users WHERE SHA1(CONCAT(`id`,`mysalt`)) = '".mysql_real_escape_string($_COOKIE['test'])."'");
if I understand correctly what you're trying to do.
The solutions already provided probably will work just fine, however are you certain you want to do this? If the field "id" is really a distinct identification you can use "LIMIT 1" to stop mysql from searching thru all your items. Another thing is, why don't you use a separate precomputed field for this? I mean in every query mysql unnecessarily needs to compute all these sha1 values.. One last thing. I'm uncertain why you are using your approach, but my best guess is to implement some sort of session key. I thing this is a bad idea for a couple of reasons: If someone gets holds on your salt, he has access to all your accounts. If someone sniffs one "session" he can reuse it whenever he wants to. Choosing a weak salt could have serious consequences. HTH.
Use CONCAT:
$sql = ("SELECT * FROM users WHERE SHA1(CONCAT(`id`,'mysalt')) = '".mysql_real_escape_string($_COOKIE[''test''])."'");