Jim R Posted May 20, 2009 Share Posted May 20, 2009 if === *_* Not that it's the proper syntax, but I'm wondering if I can extract values that only have an underscore in the field of data. Quote Link to comment https://forums.phpfreaks.com/topic/158910-can-wildcards-be-used-in-where-statements/ Share on other sites More sharing options...
Daniel0 Posted May 20, 2009 Share Posted May 20, 2009 Wildcards can be used using LIKE. SELECT username, email FROM users WHERE email LIKE '%@gmail.com'; The % is the wildcard here. http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html Quote Link to comment https://forums.phpfreaks.com/topic/158910-can-wildcards-be-used-in-where-statements/#findComment-838093 Share on other sites More sharing options...
Maq Posted May 20, 2009 Share Posted May 20, 2009 The underscore is a single character wildcard. If you want it literally you must escape it. but I'm wondering if I can extract values that only have an underscore in the field of data. That would just be an underscore by itself. Do you mean entries with underscores anywhere in the field? In this case: '%\_%' Quote Link to comment https://forums.phpfreaks.com/topic/158910-can-wildcards-be-used-in-where-statements/#findComment-838107 Share on other sites More sharing options...
Daniel0 Posted May 20, 2009 Share Posted May 20, 2009 MySQL has a function called INSTR() actually. SELECT foo FROM bar WHERE INSTR(baz, '_'); I don't know what's faster though. Quote Link to comment https://forums.phpfreaks.com/topic/158910-can-wildcards-be-used-in-where-statements/#findComment-838111 Share on other sites More sharing options...
Jim R Posted May 20, 2009 Author Share Posted May 20, 2009 I was getting ready to post that the query was returning all the values, but escaping the underscore made it work. Thanks to you both. Is there also a way to make it omit other key words, like "main" or "page". Quote Link to comment https://forums.phpfreaks.com/topic/158910-can-wildcards-be-used-in-where-statements/#findComment-838112 Share on other sites More sharing options...
Daniel0 Posted May 20, 2009 Share Posted May 20, 2009 Just add another statement: ... WHERE something LIKE '%\_%' AND something NOT LIKE '%main%' ... Quote Link to comment https://forums.phpfreaks.com/topic/158910-can-wildcards-be-used-in-where-statements/#findComment-838116 Share on other sites More sharing options...
Jim R Posted May 20, 2009 Author Share Posted May 20, 2009 Just add another statement: ... WHERE something LIKE '%\_%' AND something NOT LIKE '%main%' ... Nice. Quote Link to comment https://forums.phpfreaks.com/topic/158910-can-wildcards-be-used-in-where-statements/#findComment-838119 Share on other sites More sharing options...
Jim R Posted May 20, 2009 Author Share Posted May 20, 2009 So is this the most concise way to write this: SELECT * FROM wikirecentchanges WHERE rc_title LIKE '%\_%' AND rc_title NOT LIKE '%Main%' AND rc_title NOT LIKE '%20%' I tried commas. It didn't work. Quote Link to comment https://forums.phpfreaks.com/topic/158910-can-wildcards-be-used-in-where-statements/#findComment-838132 Share on other sites More sharing options...
Daniel0 Posted May 20, 2009 Share Posted May 20, 2009 Define "didn't work". Quote Link to comment https://forums.phpfreaks.com/topic/158910-can-wildcards-be-used-in-where-statements/#findComment-838138 Share on other sites More sharing options...
Jim R Posted May 20, 2009 Author Share Posted May 20, 2009 Define "didn't work". I tried multiple ways, and it either ignored the statement or gave me a syntax error. Quote Link to comment https://forums.phpfreaks.com/topic/158910-can-wildcards-be-used-in-where-statements/#findComment-838171 Share on other sites More sharing options...
Maq Posted May 20, 2009 Share Posted May 20, 2009 Define "didn't work". I tried multiple ways, and it either ignored the statement or gave me a syntax error. So, what's the error? Make sure you have this at the end of your mysql_query() call: or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/158910-can-wildcards-be-used-in-where-statements/#findComment-838176 Share on other sites More sharing options...
Jim R Posted May 20, 2009 Author Share Posted May 20, 2009 Ok...when I say error, I just mean within the SQLyog program I use to view and query my databases. Sorry that I omitted that point. Quote Link to comment https://forums.phpfreaks.com/topic/158910-can-wildcards-be-used-in-where-statements/#findComment-838182 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.