Unix Technical Forum

Reverse like query?

This is a discussion on Reverse like query? within the MySQL forums, part of the Database Server Software category; --> I have some problems figuring this one out. How do I put together a query that selects those rows ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 07:18 AM
Rune
 
Posts: n/a
Default Reverse like query?

I have some problems figuring this one out. How do I put together a query
that selects those rows in which a field contains a substring of another
given string.

e.g.

table test (test varchar(255))

row1: this is a test
row2: test
row3: a test
row4: that

If I have a string called "a test", then I want to select row 1 + 3, but not
2 and 4.

thanks
/Rune


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 07:18 AM
Stefan Rybacki
 
Posts: n/a
Default Re: Reverse like query?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Rune schrieb:
> I have some problems figuring this one out. How do I put together a query
> that selects those rows in which a field contains a substring of another
> given string.
>
> e.g.
>
> table test (test varchar(255))
>
> row1: this is a test
> row2: test
> row3: a test
> row4: that
>
> If I have a string called "a test", then I want to select row 1 + 3, but not
> 2 and 4.
>
> thanks
> /Rune
>
>


SELECT * FROM table WHERE test LIKE '%a test%'

Regards
Stefan
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)

iD8DBQFD2hQGyeCLzp/JKjARAgcKAJ9bnreVPkAexS77/7/HhmgKQc0oqgCdGvCS
Gy/hlNFk38SepsZgCSxPtaU=
=V8jz
-----END PGP SIGNATURE-----
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 07:18 AM
Rune
 
Posts: n/a
Default Re: Reverse like query?

"Stefan Rybacki" <stefan.rybacki@gmx.net> skrev i en meddelelse
news:43uig6F1pje2gU1@individual.net...
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Rune schrieb:
>> I have some problems figuring this one out. How do I put together a query
>> that selects those rows in which a field contains a substring of another
>> given string.
>>
>> e.g.
>>
>> table test (test varchar(255))
>>
>> row1: this is a test
>> row2: test
>> row3: a test
>> row4: that
>>
>> If I have a string called "a test", then I want to select row 1 + 3, but
>> not
>> 2 and 4.
>>
>> thanks
>> /Rune
>>
>>

>
> SELECT * FROM table WHERE test LIKE '%a test%'
>
> Regards
> Stefan


Dang! I put the question wrong. I keep mixing it up, because it's the
oppositite of what I usally would need. I need to return all those rows
where all the field is in the given string. It should return rows: 2+3, but
not 1,4

1) "this is a test" is not part of "a test" : FAULT
2) "test" is part of "a test" : OK
3) "a test" is part of "a test" : OK
4) "that" is not part of "a test": FAULT


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 07:18 AM
Rune
 
Posts: n/a
Default Re: Reverse like query?

"Rune" <slam@slam.slam> skrev i en meddelelse
news:43da150b$0$15788$14726298@news.sunsite.dk...
> "Stefan Rybacki" <stefan.rybacki@gmx.net> skrev i en meddelelse
> news:43uig6F1pje2gU1@individual.net...
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Rune schrieb:
>>> I have some problems figuring this one out. How do I put together a
>>> query
>>> that selects those rows in which a field contains a substring of another
>>> given string.
>>>
>>> e.g.
>>>
>>> table test (test varchar(255))
>>>
>>> row1: this is a test
>>> row2: test
>>> row3: a test
>>> row4: that
>>>
>>> If I have a string called "a test", then I want to select row 1 + 3, but
>>> not
>>> 2 and 4.
>>>
>>> thanks
>>> /Rune
>>>
>>>

>>
>> SELECT * FROM table WHERE test LIKE '%a test%'
>>
>> Regards
>> Stefan

>
> Dang! I put the question wrong. I keep mixing it up, because it's the
> oppositite of what I usally would need. I need to return all those rows
> where all the field is in the given string. It should return rows: 2+3,
> but not 1,4
>
> 1) "this is a test" is not part of "a test" : FAULT
> 2) "test" is part of "a test" : OK
> 3) "a test" is part of "a test" : OK
> 4) "that" is not part of "a test": FAULT


Something like this: SELECT * from test WHERE "a test" like %test%;
But this won't work.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 07:18 AM
Rune
 
Posts: n/a
Default Re: Reverse like query?

"Rune" <slam@slam.slam> skrev i en meddelelse
news:43da1619$0$15785$14726298@news.sunsite.dk...
> "Rune" <slam@slam.slam> skrev i en meddelelse
> news:43da150b$0$15788$14726298@news.sunsite.dk...
>> "Stefan Rybacki" <stefan.rybacki@gmx.net> skrev i en meddelelse
>> news:43uig6F1pje2gU1@individual.net...
>>> -----BEGIN PGP SIGNED MESSAGE-----
>>> Hash: SHA1
>>>
>>> Rune schrieb:
>>>> I have some problems figuring this one out. How do I put together a
>>>> query
>>>> that selects those rows in which a field contains a substring of
>>>> another
>>>> given string.
>>>>
>>>> e.g.
>>>>
>>>> table test (test varchar(255))
>>>>
>>>> row1: this is a test
>>>> row2: test
>>>> row3: a test
>>>> row4: that
>>>>
>>>> If I have a string called "a test", then I want to select row 1 + 3,
>>>> but not
>>>> 2 and 4.
>>>>
>>>> thanks
>>>> /Rune
>>>>
>>>>
>>>
>>> SELECT * FROM table WHERE test LIKE '%a test%'
>>>
>>> Regards
>>> Stefan

>>
>> Dang! I put the question wrong. I keep mixing it up, because it's the
>> oppositite of what I usally would need. I need to return all those rows
>> where all the field is in the given string. It should return rows: 2+3,
>> but not 1,4
>>
>> 1) "this is a test" is not part of "a test" : FAULT
>> 2) "test" is part of "a test" : OK
>> 3) "a test" is part of "a test" : OK
>> 4) "that" is not part of "a test": FAULT

>
> Something like this: SELECT * from test WHERE "a test" like %test%;
> But this won't work.


select * from test where test in ("a test") : returns row 3) but not 2)
select * from test where %test% in ("a test"): is syntax error

Alas!


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 07:18 AM
Jerry Stuckle
 
Posts: n/a
Default Re: Reverse like query?

Rune wrote:
> I have some problems figuring this one out. How do I put together a query
> that selects those rows in which a field contains a substring of another
> given string.
>
> e.g.
>
> table test (test varchar(255))
>
> row1: this is a test
> row2: test
> row3: a test
> row4: that
>
> If I have a string called "a test", then I want to select row 1 + 3, but not
> 2 and 4.
>
> thanks
> /Rune
>
>



SELECT * FROM table WHERE data LIKE '%a test';

http://dev.mysql.com/doc/refman/5.1/...functions.html

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-28-2008, 07:18 AM
Rune
 
Posts: n/a
Default Re: Reverse like query?

"Jerry Stuckle" <jstucklex@attglobal.net> skrev i en meddelelse
news:1_-dnfVB6qcXgUfeRVn-ow@comcast.com...
> Rune wrote:
>> I have some problems figuring this one out. How do I put together a query
>> that selects those rows in which a field contains a substring of another
>> given string.
>>
>> e.g.
>>
>> table test (test varchar(255))
>>
>> row1: this is a test
>> row2: test
>> row3: a test
>> row4: that
>>
>> If I have a string called "a test", then I want to select row 1 + 3, but
>> not 2 and 4.
>>
>> thanks
>> /Rune

>
>
> SELECT * FROM table WHERE data LIKE '%a test';
>
> http://dev.mysql.com/doc/refman/5.1/...functions.html


Nope. I'm sorry. I put the question wrong. I keep mixing it up, because it's
the
oppositite of what I usally would need. I need to return all those rows
where all of the field is in the given string. It should return rows: 2+3,
but
not 1,4

1) "this is a test" is not part of "a test" : FAULT
2) "test" is part of "a test" : OK
3) "a test" is part of "a test" : OK
4) "that" is not part of "a test": FAULT

Something like SELECT * from test WHERE "a test" like %test%; - but that
doesn't work on MySQL


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-28-2008, 07:18 AM
Harald Fuchs
 
Posts: n/a
Default Re: Reverse like query?

In article <43da1c84$0$15788$14726298@news.sunsite.dk>,
"Rune" <slam@slam.slam> writes:

>>> Dang! I put the question wrong. I keep mixing it up, because it's the
>>> oppositite of what I usally would need. I need to return all those rows
>>> where all the field is in the given string. It should return rows: 2+3,
>>> but not 1,4
>>>
>>> 1) "this is a test" is not part of "a test" : FAULT
>>> 2) "test" is part of "a test" : OK
>>> 3) "a test" is part of "a test" : OK
>>> 4) "that" is not part of "a test": FAULT

>>
>> Something like this: SELECT * from test WHERE "a test" like %test%;
>> But this won't work.


> select * from test where test in ("a test") : returns row 3) but not 2)
> select * from test where %test% in ("a test"): is syntax error


Use the following:

SELECT whatever
FROM test
WHERE 'a test' LIKE concat('%', test, '%')
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-28-2008, 07:18 AM
Rune
 
Posts: n/a
Default Re: Reverse like query?

"Harald Fuchs" <hf0923x@protecting.net> skrev i en meddelelse
news:87d5idrdq5.fsf@srv.protecting.net...
> In article <43da1c84$0$15788$14726298@news.sunsite.dk>,
> "Rune" <slam@slam.slam> writes:
>
>>>> Dang! I put the question wrong. I keep mixing it up, because it's the
>>>> oppositite of what I usally would need. I need to return all those rows
>>>> where all the field is in the given string. It should return rows: 2+3,
>>>> but not 1,4
>>>>
>>>> 1) "this is a test" is not part of "a test" : FAULT
>>>> 2) "test" is part of "a test" : OK
>>>> 3) "a test" is part of "a test" : OK
>>>> 4) "that" is not part of "a test": FAULT
>>>
>>> Something like this: SELECT * from test WHERE "a test" like %test%;
>>> But this won't work.

>
>> select * from test where test in ("a test") : returns row 3) but not 2)
>> select * from test where %test% in ("a test"): is syntax error

>
> Use the following:
>
> SELECT whatever
> FROM test
> WHERE 'a test' LIKE concat('%', test, '%')


Yes! Just what I need. Thanks.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:18 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com