LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-16-2007, 05:04 AM   #1
Rickead2000
LQ Newbie
 
Registered: Jul 2002
Posts: 24

Rep: Reputation: 15
help building a mysql search query


Hi,

I need to build an SQL search query to find a row that is the longest part of the start of another string.

e.g.

search string - "helloworld"

rows in database

hellowo
elloworld


I would need it to match hellowo.

My current query I'm runing is..

select * from list where helloworld RLIKE list ORDER BY LENGTH(list) DESC

However, this would actually match "elloworld" as the first result. I need the query to return only results that start as the search string, not just contain it.

Can anybody point me in the right direction?

I have tried running a query for an exact match, and if it doesn't find one, it knocks a character off the end and re runs it. This works but makes the script run very very slowly and hammers my CPU.

Last edited by Rickead2000; 10-16-2007 at 05:05 AM.
 
Old 10-17-2007, 02:34 PM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Maybe this?

select * from table where 'helloworld' like concat(list,'%') order by length(list) desc limit 1
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MYSQL search query by column name using wildcard AQG Programming 9 08-07-2007 09:41 AM
help with mysql query: return nth rows in query hawarden Programming 2 07-31-2006 06:36 PM
can my web server process search query? laeeq38 Linux - Newbie 4 07-29-2006 02:20 PM
PHP search query? cdc5205 Programming 2 03-17-2006 08:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration