Database Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesDatabase Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old December 26th, 2008, 05:09 PM
lse123 lse123 is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Nov 2007
Posts: 102 lse123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 38 m 6 sec
Reputation Power: 3
MySQL 5 - Sort the table "sql:select results query" to appear in sequence of the array $x[], wh

If I have an array created on runtime dynamically[php] like $x[5]=[2,4,3,1,5] and a database table in mysql with PK values starting from 1 to 5 , and I want to sort the table "sql:select results query" to appear in sequence of the array $x[], what process I must follow ????
first 2 row, after 4 row, after 3 row, after 1 row, and last 5 row, in this array dynamic generation ....

Reply With Quote
  #2  
Old December 26th, 2008, 05:26 PM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 2,069 IAmALlama User rank is Private First Class (20 - 50 Reputation Level)IAmALlama User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 6 Days 7 h 58 m 46 sec
Reputation Power: 5
you can use the mysql field() function to do this. the first argument would be a column name and the next arguments are a list of values. field returns the index (position) of the first argument in the rest of the arguments. so for example if column ID is equal to 5 then:

Code:
SELECT FIELD(ID, 5, 4, 1, 3, 2, 6)
//would return 1 because id=5 which is the
//1st argument after the column.
SELECT FIELD(ID, 4,2,3,1,7,8,6,5)
//would return 8 because 5 is the 8th number in the list.

using this, you can order by your custom list using the return value from field to sort.
PHP Code:
//the order you want to sort in an array
$sortArray = array(5,4,1,2,3);
//turn that array into a string for use in the query.
$sortString implode(','$sortArray);

$query "SELECT * FROM TableName ORDER BY FIELD(ID,$sortString)";
echo 
"your query is: $query<br />";
$result mysql_query($query) or die("There was an error: ".mysql_error());
echo 
"the result is:<br /><pre>";
while(
$row mysql_fetch_array($result)){
    
print_r($row);


just a warning too, if the column you are trying to sort is not in the list of arguments, then field will return 0 meaning it will be at the top of the list. if you want it to be at the bottom, then reverse the array order and sort desc.

Reply With Quote
  #3  
Old January 3rd, 2009, 04:39 AM
lse123 lse123 is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Nov 2007
Posts: 102 lse123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 38 m 6 sec
Reputation Power: 3
is needed the array element if are 5 to be 1,5,4,2,3 ? can be

is needed the array element if are 5 to be 1,5,4,2,3 ? can be 9,5,2,6,13 ?

what if I have an array of 6, 9, 8,1,5 booking id, and I want corresponding names of customers to booking id used elsewhere (get values) what do ?

I can NOT use multiple
foreach ($_POST['c'] as $n) { // $_POST['c']=array bookingid
$query[$n]="SELECT * FROM BOOKINGS WHERE bookingid='".$n."'";
}

what use then ?

Reply With Quote
  #4  
Old January 3rd, 2009, 04:45 AM
lse123 lse123 is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Nov 2007
Posts: 102 lse123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 38 m 6 sec
Reputation Power: 3
In less words what do if SELECT...WHERE bookingid=array[]; ?

In less words what do if SELECT...WHERE bookingid=array[]; ?

Reply With Quote
  #5  
Old January 3rd, 2009, 03:58 PM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 2,069 IAmALlama User rank is Private First Class (20 - 50 Reputation Level)IAmALlama User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 6 Days 7 h 58 m 46 sec
Reputation Power: 5
you can use the 'IN' keyword.

Code:
SELECT * FROM SomeTable WHERE ID IN (4,5,1,3,2,13)


its just where column IN ('comma', 'separated', 'list', 'of', 'values').

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesDatabase Help > MySQL 5 - Sort the table "sql:select results query" to appear in sequence of the array $x[], wh


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 12 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek