473,471 Members | 2,533 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Ranking by fields and then order by

Hi,

I have a table with a list of companies, with fields like this
(simplyfied):

ID (int, auto_increment)
CompanyName (text)
ContactPerson (text)
Description (text)
Partner (int)

Listing example:

ID:
15

CompanyName:
BeWide Technologies

ContactPerson:
Morten Tangeraas

Description:
Developing websites and internet applications and programming in php
with mysql.

Partner:
30
They are grouped in partner levels, where the highest priored partners
has ID 40 (in the Partner field) and the lowest has ID 10.

I am currently using a query like this to search the database (example
search string is foo bar):

SELECT * FROM Companies WHERE (CompanyName like '%foo%' OR
ContactPerson like '%foo%' OR Description like '%foo%') AND
(CompanyName like '%bar%' OR ContactPerson like '%bar%' OR Description
like '%bar%') ORDER BY Partner DESC

In many cases, this query doesn't give the most relevant result. What
can I do with my query to make a priority like this:

1. Companies with highest partner number first (this one is ok)

2. Companies with "foo" early in the CompanyName field, then companies
with "foo" later in the CompanyName field

3. The same as in point 2, only in the ContactPerson field, then the
Description field.

4. Then the same as in point 2 and 3, only with the word "bar"

How can I obtain this result with mySQL?

Thanks in advance!

Regards,
Morten Tangeraas
BeWide Technologies, Norway

Aug 10 '06 #1
3 3494

ma*******@gmail.com wrote:
Hi,

I have a table with a list of companies, with fields like this
(simplyfied):

ID (int, auto_increment)
CompanyName (text)
ContactPerson (text)
Description (text)
Partner (int)

Listing example:

ID:
15

CompanyName:
BeWide Technologies

ContactPerson:
Morten Tangeraas

Description:
Developing websites and internet applications and programming in php
with mysql.

Partner:
30
They are grouped in partner levels, where the highest priored partners
has ID 40 (in the Partner field) and the lowest has ID 10.

I am currently using a query like this to search the database (example
search string is foo bar):

SELECT * FROM Companies WHERE (CompanyName like '%foo%' OR
ContactPerson like '%foo%' OR Description like '%foo%') AND
(CompanyName like '%bar%' OR ContactPerson like '%bar%' OR Description
like '%bar%') ORDER BY Partner DESC

In many cases, this query doesn't give the most relevant result. What
can I do with my query to make a priority like this:

1. Companies with highest partner number first (this one is ok)

2. Companies with "foo" early in the CompanyName field, then companies
with "foo" later in the CompanyName field

3. The same as in point 2, only in the ContactPerson field, then the
Description field.

4. Then the same as in point 2 and 3, only with the word "bar"

How can I obtain this result with mySQL?

Thanks in advance!

Regards,
Morten Tangeraas
BeWide Technologies, Norway
Locate('foo',CompanyName) returns 0 if the searchstring is not present
- which could confuse the results - but there's probably a workaround
for that.

Aug 10 '06 #2

strawberry wrote:
ma*******@gmail.com wrote:
Hi,

I have a table with a list of companies, with fields like this
(simplyfied):

ID (int, auto_increment)
CompanyName (text)
ContactPerson (text)
Description (text)
Partner (int)

Listing example:

ID:
15

CompanyName:
BeWide Technologies

ContactPerson:
Morten Tangeraas

Description:
Developing websites and internet applications and programming in php
with mysql.

Partner:
30
They are grouped in partner levels, where the highest priored partners
has ID 40 (in the Partner field) and the lowest has ID 10.

I am currently using a query like this to search the database (example
search string is foo bar):

SELECT * FROM Companies WHERE (CompanyName like '%foo%' OR
ContactPerson like '%foo%' OR Description like '%foo%') AND
(CompanyName like '%bar%' OR ContactPerson like '%bar%' OR Description
like '%bar%') ORDER BY Partner DESC

In many cases, this query doesn't give the most relevant result. What
can I do with my query to make a priority like this:

1. Companies with highest partner number first (this one is ok)

2. Companies with "foo" early in the CompanyName field, then companies
with "foo" later in the CompanyName field

3. The same as in point 2, only in the ContactPerson field, then the
Description field.

4. Then the same as in point 2 and 3, only with the word "bar"

How can I obtain this result with mySQL?

Thanks in advance!

Regards,
Morten Tangeraas
BeWide Technologies, Norway

Locate('foo',CompanyName) returns 0 if the searchstring is not present
- which could confuse the results - but there's probably a workaround
for that.
Here's a very crude workaround. Shows how bad I am at maths!

SELECT *,

1000-((Locate('foo',CompanyName)/LOCATE('foo',CompanyName))*LOCATE('foo',Company))
as cn_pos

FROM Companies WHERE (CompanyName like '%foo%' OR
ContactPerson like '%foo%' OR Description like '%foo%') AND
(CompanyName like '%bar%' OR ContactPerson like '%bar%' OR Description
like '%bar%') ORDER BY Partner DESC,cn_pos DESC;

obviously just copy/paste for the other fields

Aug 10 '06 #3
Thank you for the reply, it seem to work just fine! Have to try it out
with the database a little to see if it gives the wanted results...
but, what is LOCATE acctually doing? I can't find any good
documentation of it...

Regards,
Morten

strawberry wrote:

[...]
SELECT *,

1000-((Locate('foo',CompanyName)/LOCATE('foo',CompanyName))*LOCATE('foo',Company))
as cn_pos

FROM Companies WHERE (CompanyName like '%foo%' OR
ContactPerson like '%foo%' OR Description like '%foo%') AND
(CompanyName like '%bar%' OR ContactPerson like '%bar%' OR Description
like '%bar%') ORDER BY Partner DESC,cn_pos DESC;

obviously just copy/paste for the other fields
Aug 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Irene | last post by:
Hi all again, Well, I have my Athletics database with Athletes, Competitions, Scores tables. I have a ranking query where I get back the list of the competitions-athletes and scores...
5
by: ED | last post by:
I currently have vba code that ranks employees based on their average job time ordered by their region, zone, and job code. I currently have vba code that will cycle through a query and ranks each...
1
by: Joseph Bloch | last post by:
In all the threads on ranking in Access queries, I've not seen anything that deals with ranking against a subset of the query results. What I need to do is take the following query results: Dept...
0
by: Jeff Boes | last post by:
I hope this helps someone else ... I had struggled some time ago with attempts to get a rank of values query to work, but then I gave up and set it aside. I had another reason to attack it, and in...
5
by: valglad | last post by:
Hi, The question below was posted about 4 years ago and noone was able to answer it back then. I have virtually the same type of problem so would appreciate if anyone can help. Thanks ...
6
by: sara | last post by:
I hope someone can help with this. Our director wants to have a report that will have the departments (Retail stores) across the top, stores down the side and the RANKING of the YTD dept sales...
5
by: Chris | last post by:
I was wodering if there was a way to rank numbers in a query like this #'s Rank 100 1 99 2 98 3 98 97 5 96 6 96
1
by: OtheymAverian | last post by:
For the past few days I have been trying to figure out how to add a Ranking field to a sorted query so I can then reference the rank in a report. I am using Access 2002 and have no SQL experience,...
0
debasisdas
by: debasisdas | last post by:
Rank:-assigns A Unique Number For Each Row Starting With 1,except For Rows That Have Duplicate Values,in Which Case The Same Ranking Is Assigned And A Gap Appears In The Sequence For Each Duplicate...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.