Solved

mysql select value where contains string but not with space after

Posted on 2010-11-21
10
915 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
INDEX does not make a difference, why? 10 73
Using cfstoredproc to return query data 2 69
reverse engineer .sql from php files 11 67
Problem with MySQL query - graph 3 47
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…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 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