limitphp Posted January 16, 2009 Share Posted January 16, 2009 select * from artist where artistName LIKE '%es%' The above query will return all artists that contain "es" in their name. A whole bunch, basically. How would I modify the search to just show me artists that contain the word es in their name? Lets say there are two bands with weird names like "ES Buzz" and "Through ES". Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/ Share on other sites More sharing options...
revraz Posted January 16, 2009 Share Posted January 16, 2009 Well if you want to to Start or End with ES, you can do es% and %es. Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-738459 Share on other sites More sharing options...
limitphp Posted January 16, 2009 Author Share Posted January 16, 2009 Well if you want to to Start or End with ES, you can do es% and %es. but then I would have to do 2 different searches to get the above results. PLus I would get alot of irrelevant results like strokes, fires, etc Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-738465 Share on other sites More sharing options...
dennismonsewicz Posted January 16, 2009 Share Posted January 16, 2009 try this select * from artist where artistName LIKE '%es' OR artistName LIKE 'es%' Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-738466 Share on other sites More sharing options...
limitphp Posted January 16, 2009 Author Share Posted January 16, 2009 try this select * from artist where artistName LIKE '%es' OR artistName LIKE 'es%' isn't that the same as LIKE '%es%'? Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-738467 Share on other sites More sharing options...
revraz Posted January 16, 2009 Share Posted January 16, 2009 So you want to have only things with ES (space) and (space) ES returned? But no, you wouldn't have to do two different searches, it can be done with 1 query, that's why they make AND and OR Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-738468 Share on other sites More sharing options...
revraz Posted January 16, 2009 Share Posted January 16, 2009 No, but it would return what you said above, with things that end with es and start with es. %es% would return even if in the middle. try this select * from artist where artistName LIKE '%es' OR artistName LIKE 'es%' isn't that the same as LIKE '%es%'? Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-738469 Share on other sites More sharing options...
limitphp Posted January 16, 2009 Author Share Posted January 16, 2009 So you want to have only things with ES (space) and (space) ES returned? Yes. I want all results that contain the word "es". I tried doing a LIKE '% es %' but if the artistName is "ES Buzz", it won't return it, because there is no space before es. Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-738472 Share on other sites More sharing options...
limitphp Posted January 16, 2009 Author Share Posted January 16, 2009 No, but it would return what you said above, with things that end with es and start with es. %es% would return even if in the middle. Oh yeah...true. Again, though. It would return irrelevant results like strokes, fires, etc. I tried LIKE 'es' without the %, but it didn't seem to work either. Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-738473 Share on other sites More sharing options...
revraz Posted January 16, 2009 Share Posted January 16, 2009 select * from artist where artistName LIKE '%es ' OR artistName LIKE ' es%' Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-738483 Share on other sites More sharing options...
limitphp Posted January 16, 2009 Author Share Posted January 16, 2009 Basically, what I want to do is search a column that contains an exact word, or contains the exact word plus other words. Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-738485 Share on other sites More sharing options...
revraz Posted January 16, 2009 Share Posted January 16, 2009 Try what I posted. Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-738489 Share on other sites More sharing options...
limitphp Posted January 16, 2009 Author Share Posted January 16, 2009 select * from artist where artistName LIKE '%es ' OR artistName LIKE ' es%' Try what I posted. ok. I'm not getting any results now. Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-738493 Share on other sites More sharing options...
revraz Posted January 16, 2009 Share Posted January 16, 2009 You need to change it to adjust for your table and fieldnames. Post your actual code. Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-738495 Share on other sites More sharing options...
limitphp Posted January 16, 2009 Author Share Posted January 16, 2009 select * from artist where artistName LIKE '%es ' OR artistName LIKE ' es%' Post your actual code. I'm actually doing it in the phpadmin. i'm testing the sql. here's what I'm typing in: select artistName from artist where artistName LIKE '%es ' OR artistName LIKE ' es%' I added "ES Buzz" and "Through ES" to the artist table. still getting no results. Problem is, just looking at the code, I assume it will give me anything that ends with es or anything that begins with es. What I want is anything that contains exactly the query word or contains exactly the query word plus other words. Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-738536 Share on other sites More sharing options...
limitphp Posted January 16, 2009 Author Share Posted January 16, 2009 I guess I could word it a different way.... What I want is to do this: select * from artist where artistName = "$word" only I want all of those results from above, plus any results that contain the $word plus other words. Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-738554 Share on other sites More sharing options...
limitphp Posted January 16, 2009 Author Share Posted January 16, 2009 Maybe its not a LIKE operator that I need. As, LIKE suggests, doing just that...finding anything that is like the word. Are there only =, like and match to use for searching? Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-738582 Share on other sites More sharing options...
premiso Posted January 16, 2009 Share Posted January 16, 2009 It is hard to determine that but: select artistName from artist where artistName LIKE 'es ' OR artistName LIKE ' es' Would/should work for just the ES, the % (wild) card is optional. Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-738640 Share on other sites More sharing options...
limitphp Posted January 20, 2009 Author Share Posted January 20, 2009 It is hard to determine that but: select artistName from artist where artistName LIKE 'es ' OR artistName LIKE ' es' Would/should work for just the ES, the % (wild) card is optional. Problem with that is, lets say they search for a band named Watercolors, with that search it looks for watercolors with a space after it, and watercolors with a space before it. Well, in the database, artistName for the band watercolors is not going to have any spaces, just the word Watercolors, so it actually doesn't return that record. Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-741168 Share on other sites More sharing options...
revraz Posted January 20, 2009 Share Posted January 20, 2009 I've already showed you how to do it, so I'm not sure what your current question is now. Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-741170 Share on other sites More sharing options...
limitphp Posted January 20, 2009 Author Share Posted January 20, 2009 I've already showed you how to do it, so I'm not sure what your current question is now. One of us has misunderstood something here. I'm trying to do a query that searches for a $word and matches records containing exactly that $word or records containing exactly that word plus other words. I have yet to see any post in here that shows me how to do that. What I'm gathering here, is that a simple query that uses LIKE cannot do that. Hopefully, I'm wrong about that (I'm pretty much a newbie when it comes to MYSQL), but I'm starting to think that is the case. Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-741182 Share on other sites More sharing options...
revraz Posted January 20, 2009 Share Posted January 20, 2009 How is this not working? select * from artist where artistName LIKE '%es ' OR artistName LIKE ' es%' Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-741188 Share on other sites More sharing options...
limitphp Posted January 20, 2009 Author Share Posted January 20, 2009 How is this not working? select * from artist where artistName LIKE '%es ' OR artistName LIKE ' es%' If someone were to type in "Watercolors", it will not return the band Watercolors because in the artistName field for Watercolors there is no space after Watercolors and there is no space before Watercolors. Basically, that query would fail to return all results of bands where artistname contained only 1 word. Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-741212 Share on other sites More sharing options...
revraz Posted January 20, 2009 Share Posted January 20, 2009 select * from artist where artistName = '$name' OR LIKE '%{$name} ' OR artistName LIKE ' {$name}%' Something like that may work. Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-741373 Share on other sites More sharing options...
limitphp Posted January 20, 2009 Author Share Posted January 20, 2009 select * from artist where artistName = '$name' OR LIKE '%{$name} ' OR artistName LIKE ' {$name}%' Something like that may work. that doesn't seem to work either. What exactly are the braces supposed to do? Quote Link to comment https://forums.phpfreaks.com/topic/141092-help-with-like-search/#findComment-741467 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.