Solved

mysql select value where contains string but not with space after

Posted on 2010-11-21
10
910 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
ID: 34182820
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
ID: 34182848
try the following fieldname <> enter then value you dont want in output
0
 

Author Comment

by:freshwaterwest
ID: 34182851
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 6

Accepted Solution

by:
brb6708 earned 200 total points
ID: 34182855
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
ID: 34182878
--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
 
LVL 8

Expert Comment

by:pdd1lan
ID: 34182889
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
ID: 34182911
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
ID: 34182925
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
ID: 34182926
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
ID: 34182977
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

825 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