2

I want a MySQL query to return rows which don't have the only input string/phrase in it.

Example: I have table with the column name like

Sno  | sname
---------------
1    | AAA
2    | SSS
3    | Group Post
4    | Group Post, Test Run
5    | Group Post, Group Post
6    | Group Post, Group Post, My test Run

In the above table I want to eliminate the rows which have "Group Post" only in them. That means how many times Group Post exist in the row and there are no other phrases or words in them, then we can eliminate it.

So I want a query which will return

Sno  | sname
---------------
1    | AAA
2    | SSS
4    | Group Post, Test Run
6    | Group Post, Group Post, My test Run

In these rows some of them have Group Post in them. But along with the that we have other phrases in them.

Thanks in Advance.

4
  • 1
    Sorry, but your question is very hard to decipher as written. Do you want rows 3 to be eliminated? Or more than that? Please provide an "after" example to go with your "before" example, I'm sure that will help clarify what you're looking to do. Commented Feb 14, 2014 at 11:35
  • Why can't you try it in php by getting all the row values and check it store which values were required Commented Feb 14, 2014 at 11:44
  • I'd use PHP, get the values in an array and explode the sname - go through the resulting array and pick and choose which I keep or throw out Commented Feb 14, 2014 at 11:49
  • @Kzqai: I want to fetch only the results like you edited in the post. 1,2,4,6. Commented Feb 14, 2014 at 12:07

4 Answers 4

5

You need REPLACE function to filter the data.

Try this:

select * from my_table
 where length( replace( replace( sname, 'Group Post', '' ), ', ', '' ) ) > 0

Refer to:
MySQL: REPLACE(str,from_str,to_str) Function

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Ravinder, Thanks for the Answer. Your Query Solved my problem.
0

try a subquery for finding all the ids which have the string and then use not in for example

SELECT * FORM {table_name} where id not in (select * from {table_name} where sname = "YOUR_STRING");

1 Comment

This will not solve the OP problem. Plz read the question clearly
0

select Sno,sname from table_name where length(replace(sname,'Group Post','')) > 0;

Comments

0

QUERY

SELECT *
FROM test
WHERE sname NOT LIKE '%Group Post'

SQL FIDDLE DEMO

hope this will help you...!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.