-1

I have an array of ids that are unique in the context of the table for each row. What I would like to do is to query the database and obtain ordered by date of insertion (it is already a field in the table) but only those tables which id is in the array

$query = SELECT this,andthis,andthis,andthis FROM table WHERE id=(inside array of ids)? ORDER BY date_of_insertion;

The result is to be fetch_assoc() and therefore obtaining a associative array. What I would really want would be something like this in the final array:

[1] id, this, andthis,andthis,andthis

[2] id, this, andthis,andthis,andthis

[3] ...

even if the table has other ids and rows that just weren't queried because they weren't in the specified array. Those that were queried should be ordered by time. Any ideas on how to achieve the best performance on this?

1 Answer 1

0

You can try this one:

// this is the array of ids
$array_of_ids = array();

// joins the ids in the array into a string, separating each id with a comma
$ids = implode(',', $array_of_ids); 

$query = "SELECT this, andthis, andthis, andthis FROM table WHERE id IN (" . $ids . ") ORDER BY date_of_insertion";
1
  • yeah, nicely thought! I'll try it and let you know if it worked out.
    – user111671
    Jan 12, 2014 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.