Solved

How do I select a string that contains a sub-string but doesn't end with the same sub-string in MySQL?

Posted on 2013-11-05
6
294 Views
Last Modified: 2013-11-11
How do I select a string that contains a sub-string but doesn't end with the same sub-string
I was thinking something like this, but it doesn't work, it just ends up returning all results that contain the sub-string:
u.url_sef LIKE CONCAT( '%', c.alias, '%' )
AND u.url_sef NOT LIKE CONCAT( '%', c.alias )

SELECT b.url_sef
FROM health3_mt_cat_acesef_bridge AS b, 
health3_mt_cats AS c, 
health3_acesef_urls AS u, 
health3_acesef_metadata AS m, 
health3_acesef_sitemap AS s
WHERE b.id = u.id
AND u.url_sef = m.url_sef
AND u.url_sef = s.url_sef
AND (
s.published =0
OR m.published =0
)
AND u.url_sef LIKE CONCAT( '%', c.alias, '%' )
AND u.url_sef NOT LIKE CONCAT( '%', c.alias ) 

Open in new window

0
Comment
Question by:jwleys
[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
6 Comments
 
LVL 11

Expert Comment

by:Manish
ID: 39625343
Can you try with right function to check for not end with.
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_right
0
 

Author Comment

by:jwleys
ID: 39625436
Thank you for your suggestion, I tried:

c.alias <> RIGHT( u.url_sef, LENGTH( c.alias ) )

Had the same result returned strings that contain the sub-string, didn't exclude strings that end with the sub-string. I think I am probably doing something wrong. Thank you for taking the time to help me :)

SELECT b.url_sef
FROM health3_mt_cat_acesef_bridge AS b, health3_mt_cats AS c, health3_acesef_urls AS u, health3_acesef_metadata AS m, health3_acesef_sitemap AS s
WHERE b.id = u.id
AND u.url_sef = m.url_sef
AND u.url_sef = s.url_sef
AND (
s.published =0
OR m.published =0
)
AND u.url_sef LIKE CONCAT( '%', c.alias, '%' )
AND c.alias <> RIGHT( u.url_sef, LENGTH( c.alias ) ) 

Open in new window

0
 
LVL 41

Expert Comment

by:Sharath
ID: 39635331
I did not see any issue. can you post some sample data where it is not working.
Here is example where it is working.
http://sqlfiddle.com/#!9/6c15c/3
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 25

Accepted Solution

by:
Tomas Helgi Johannsson earned 500 total points
ID: 39637558
Hi!

Line 14 in your example selects both the

This sql removes the records where  c.alias is at the end of the string in u.url_sef

SELECT b.url_sef
FROM health3_mt_cat_acesef_bridge AS b, 
health3_mt_cats AS c, 
health3_acesef_urls AS u, 
health3_acesef_metadata AS m, 
health3_acesef_sitemap AS s
WHERE b.id = u.id
AND u.url_sef = m.url_sef
AND u.url_sef = s.url_sef
AND (
s.published =0
OR m.published =0
)
AND u.url_sef LIKE CONCAT( '%', c.alias, '%' )
AND u.url_sef SUBSTRING(u.url_sef,LENGTH(u.url_sef)-LENGTH(c.alias)+1,LENGTH(u.url_sef)) <> c.alias

Open in new window


Hope this helps.

Regards,
    Tomas Helgi
0
 

Author Closing Comment

by:jwleys
ID: 39639666
I ended up using the SQL below which worked for me, your logic was correct but contained a syntax error :

"AND u.url_sef SUBSTRING(u.url_sef,LENGTH(u.url_sef)-LENGTH(c.alias)+1,LENGTH(u.url_sef)) <> c.alias"

When removing "u.url_sef" from the AND clause the query worked perfectly,  thanks :)

SELECT DISTINCT b.url_sef
FROM health3_mt_cat_acesef_bridge AS b,
health3_mt_cats AS c,
health3_acesef_urls AS u,
health3_acesef_metadata AS m,
health3_acesef_sitemap AS s
WHERE b.id = u.id
AND u.url_sef = m.url_sef
AND u.url_sef = s.url_sef
AND (
s.published =0
OR m.published =0
)
AND u.url_sef LIKE CONCAT( '%', c.alias, '%' )
AND SUBSTRING(u.url_sef,LENGTH(u.url_sef)-LENGTH(c.alias)+1,LENGTH(u.url_sef)) <> c.alias
0
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 39639808
Hi!

Glad to help.
However a good cup of coffee  would have been nice before I wrote this. ;)
What I ment to say is : Line 14 in your example gives you  c.alias somewhere in the string including c.alias at the end of the string.
So the substring line I added to your query (and was meant to be as you corrected ) removes those cases.

Regards,
   Tomas Helgi
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

726 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