Solved

mysql select value where contains string but not with space after

Posted on 2010-11-21
10
919 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

623 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