|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 .... |
|
#2
|
|||
|
|||
|
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:
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. |
|
#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 ? |
|
#4
|
|||
|
|||
|
In less words what do if SELECT...WHERE bookingid=array[]; ?
In less words what do if SELECT...WHERE bookingid=array[]; ?
|
|
#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'). |
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Database Help > MySQL 5 - Sort the table "sql:select results query" to appear in sequence of the array $x[], wh |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|