Solved

mysql select value where contains string but not with space after

Posted on 2010-11-21
10
908 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
 
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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to change the query from oracle to  mysql. 3 58
Generate report pulling data (links) from three tables 31 59
Need help with a query 6 67
mysql left join sentence 7 36
All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
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…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

17 Experts available now in Live!

Get 1:1 Help Now