Solved

mysql select value where contains string but not with space after

Posted on 2010-11-21
10
907 Views
Last Modified: 2012-06-27
I'm really stuck on something which is probably really easy...so apologies in advance!

I have a column which can contain any of the following for instance:

Thin
or
Super Thin
or a set of values with pipe delimiters
Thin||Super Thin||Other Value||Another Value

I want to select only occurrences of Thin but not Super Thin

I've tried LIKE AND NOT LIKE but this doesn't seem to work:

SELECT value FROM table_name WHERE value LIKE "%Thin%" AND value NOT LIKE "%Super Thin%"

Also looked into REGEXP but not sure how to use in this case.

many thanks


0
Comment
Question by:freshwaterwest
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 8

Assisted Solution

by:pdd1lan
pdd1lan earned 200 total points
Comment Utility
change OR instead AND

SELECT value FROM table_name WHERE value LIKE "%Thin%" OR value NOT LIKE "%Super Thin%"
0
 
LVL 11

Assisted Solution

by:mattibutt
mattibutt earned 100 total points
Comment Utility
try the following fieldname <> enter then value you dont want in output
0
 

Author Comment

by:freshwaterwest
Comment Utility
Hi pdd1Ian, thanks for reply, I'm trying to show a list of features for a product so a product might have just "Thin" or just "Super Thin" or both Thin||Super Thin

the problem is Thin is within both values (Thin and Super Thin) so I need to find a way to make sure it's just finding Thin

hope that makes sense..

thanks
0
 
LVL 6

Accepted Solution

by:
brb6708 earned 200 total points
Comment Utility
seems ok for me. I tried it myself:

mysql> select * from test;
+---------------+
| testfeld      |
+---------------+
| Super Mario   |
| Mario         |
| Super Toll    |
| xyz|SuperThin |
| Thin          |
| Super Thin    |
+---------------+
mysql> select * from test where testfeld like '%super%';
+---------------+
| testfeld      |
+---------------+
| Super Mario   |
| Super Toll    |
| xyz|SuperThin |
| Super Thin    |
+---------------+
mysql> select * from test where testfeld like '%super%' and testfeld not like '%thin%';
+-------------+
| testfeld    |
+-------------+
| Super Mario |
| Super Toll  |
+-------------+
mysql> select * from test where testfeld like '%super%' and testfeld not like '%SuperThin%';
+-------------+
| testfeld    |
+-------------+
| Super Mario |
| Super Toll  |
| Super Thin  |
+-------------+

I can't see what's wrong with it.
0
 
LVL 8

Assisted Solution

by:pdd1lan
pdd1lan earned 200 total points
Comment Utility
--thin list
SELECT value
 FROM table_name
WHERE value  not LIKE "%Supper%"

--supper thin list
SELECT value
 FROM table_name
WHERE value   LIKE "%Supper%"
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 8

Expert Comment

by:pdd1lan
Comment Utility
or also working as this way
--thin list
SELECT value
 FROM table_name
WHERE value  <>"Supper Thin"

--supper thin list
SELECT value
 FROM table_name
WHERE value  ="Supper Thin"
0
 

Author Comment

by:freshwaterwest
Comment Utility
I can see that it all makes sense and should be working, but it doesn't work in my instance - I'm baffled - here's the details of a query I've tried:

tablename contents
id: 145792      
otherid: 20
contentid: 6536      
value: Glass||CR39||NXT||Thin||Super Thin

sql query:
SELECT value FROM tablename WHERE value LIKE "%Thin%" AND value NOT LIKE "%Super Thin%" AND contentid =  6536 AND otherid = 20

but result is nothing.

sql query:
SELECT value FROM modx_site_tmplvar_contentvalues WHERE value LIKE "%Thin%" AND contentid =  6536 AND tmplvarid = 20

gives me result:
Glass||CR39||NXT||Thin||Super Thin

as expected


0
 

Author Comment

by:freshwaterwest
Comment Utility
sorry I left the actual table names etc in the last one by mistake just to confuse things even more!
0
 
LVL 6

Assisted Solution

by:brb6708
brb6708 earned 200 total points
Comment Utility
Seems reasonable for me. You're asking for a record having content Id 6536 but not contaning 'Super Thin"in value.

The Record with contentid 6536 has "Super Thin" in value so it's correct that it's not shown using your query!

So I think your query should be adapted to that what you really want to get.
0
 

Author Comment

by:freshwaterwest
Comment Utility
thanks for your help all - I have sussed out what my problem was, partly due to me not understanding how the LIKE returns the result and partly as I had both logos showing in both cases!

I'll share the points as all comments have been correct!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now