Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 06-12-2006, 07:51 AM   #1 (permalink)
steph php
Registered User
 
Join Date: Jun 2006
Posts: 2
steph php is on a distinguished road
SQL Help !!!

Hi

i am writing an sql statement to search a table of users. in the users table are the fields location1 location2 and location3. when someone searches for 'london' i need it to check each field. which i have done, however, what i really really want is for all the people who have 'london' in location1 to come up 1st then location2 people and so on. (prioritised search results)

all this in one query would be ideal but any help with this would be much appreciated !!!

Cheers guys/gals.
steph php is offline   Reply With Quote
Old 06-12-2006, 10:53 AM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
sde is on a distinguished road
i don't think this is possible. my guess is that you would have to use 3 queries and create the sort login in the programming language. (php i'm guessing by your name)

someone please correct me if i'm wrong.
__________________
Mike
sde is offline   Reply With Quote
Old 06-12-2006, 11:18 AM   #3 (permalink)
steph php
Registered User
 
Join Date: Jun 2006
Posts: 2
steph php is on a distinguished road
Thanks

I thought i might have to do that, and yes i am using php.

i'll maybe look at an alternative solution maniplating the results from three seperate queries using php instead.

thanks
steph php is offline   Reply With Quote
Old 06-12-2006, 11:39 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
sde is on a distinguished road
if you were to use php to do it that way, it might look something like this:
PHP Code:
<?
$contacts 
= array();

$result mysql_query("select * from table where location1='london'");
while (
$row mysql_fetch_assoc) {
  
$contacts[] = $row;
}

$result mysql_query("select * from table where location2='london' AND location1 != 'london");
while (
$row mysql_fetch_assoc) {
  
$contacts[] = $row;
}

$result mysql_query("select * from table where location3='london' AND location1 != 'london" AND location2 != 'london');
while (
$row mysql_fetch_assoc) {
  
$contacts[] = $row;
}

// then maybe some more logic to check for duplicates
?>
__________________
Mike
sde is offline   Reply With Quote
Old 06-12-2006, 01:38 PM   #5 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 287
technobard is on a distinguished road
If you normalize the structure of the database, you can do this with a single query. In other words, break out location1,location2,location3.... into its own table. A side benefit is that you can have location4, etc. without changing anything.

TABLE1 (your existing table)
ID_T1 (primary key)
...

TABLE2 (new location table)
ID_T1 (part 1 of primary key - foreign key to TABLE1)
SEQ_NUM (part 2 of primary key)
LOCATION

The query would look something like:
select table1.field1, table1.field2,...table2.location from table1, table2
where table1.id_t1 = table2.id_t1
and table2.location = 'LONDON'
order by table2.seq_num

I don't do a lot of MySQL, so I'm a little rusty on ANSI JOIN syntax. You may have to modify the above to conform to those rules. Not a big deal.

The "order by..." will sort by seq_num which will be 1,2,3,.... so that when 'LONDON' shows up in seq_num = 1 (the same as LOCATION1), it occurs before seq_num = 2, etc. The catch, of course, is that you have to modify your code for adding new entries to handle the second table.
__________________
It takes 2 points to draw a straight line, but at least 3 points to draw a conclusion.
technobard is offline   Reply With Quote
Reply

Bookmarks

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
help with SQL and VB.NET doobiwan Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...) 4 06-10-2005 07:09 AM
sql server 2000 validation problems cmartin2 MS Technologies ( ASP, VB, C#, .NET ) 0 12-11-2004 11:55 AM
New to SQL and databases rmill9681 Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...) 3 09-09-2004 08:31 AM


All times are GMT -8. The time now is 02:22 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting