简体   繁体   中英

How to INSERT values of indexed array to a MySQL table in the same order as the array

Let me first preface this by saying I'm new to MySQL!

I'm trying to build a Drupal module that creates a custom list of options for my product attributes. I have an indexed array of dates in descending order and for every date value in the array the query below is supposed to create an attribute option, which is stored in the 'name' field (varchar) of my uc_attribute_options table. However, when I look at the list output or at my 'name' field (looking at it in 'Sort by key: None'), the date values in the output list appear in alphabetical order instead of the same descending order as in my array. I've tried every combination I could think of but I can't seem to "sort" this out.

$aid = 1;
$i = 0;

$date_array = array ( 
"Thursday, August 25, 2011", 
"Thursday, September 1, 2011", 
"Thursday, September 8, 2011", 
"Thursday, September 15, 2011",
"Thursday, September 22, 2011");

foreach ($date_array as $item) {

db_query("INSERT INTO {uc_attribute_options} (aid, name, cost, price,
weight, ordering) VALUES (%d, '%s', %f, %f, %f, %d)", $aid, $item, 0, 0, 0, 0);

...
}

Output:

Thursday, August 25, 2011
Thursday, September 1, 2011
Thursday, September 15, 2011
Thursday, September 22, 2011
Thursday, September 8, 2011

What I would like:

Thursday, August 25, 2011
Thursday, September 1, 2011
Thursday, September 8, 2011
Thursday, September 15, 2011
Thursday, September 22, 2011

How can I get the output to display in the same descending order as is found in the array?

Thanks in advance for any pointers.

<insert comment about row order here>

Ok, so I'm writing this because it is a legit question, even if the application is off (shouldn't matter in a relational database (even with auto_increment), yada yada yada):

$date_array = array ( 
"Thursday, August 25, 2011", 
"Thursday, September 1, 2011", 
"Thursday, September 8, 2011", 
"Thursday, September 15, 2011",
"Thursday, September 22, 2011");

$out = array();
// create an associative array which maps numeric timestamps to the dates.
foreach( $date_array as $date )
    $out[ strtotime( $date ) ] = $date;
// sort the keys (the timestamps)
ksort( $out );

// this will output in timestamp order.
foreach( $out as $item )
{
    db_query("INSERT INTO {uc_attribute_options} (aid, name, cost, price, weight,
       ordering) VALUES (%d, '%s', %f, %f, %f, %d)", $aid, $item, 0, 0, 0, 0);
}

Rows in a database have no inherent order. If you do not specify an order when you select them, the database is free to return them in any order it wants. That will not necessarily be insertion order. If you want your output in a specific order, you have to ask for it in that order, not change how you insert the data.

If you want help with that, the code to share is the part that selects the data and displays it, not the part that inserts rows.

The order of the things going into the database is usually irrelevant, if this is stored as a date you can use the ORDER BY query to get things in a particular order when retrieving. For example:

SELECT date FROM my_table ORDER BY date ASC; -- or in some cases ASCENDING

You should also be storing a Date type and not a string, this way comparison becomes much easier and strings will sort alphabetically which won't give you the results you want.

There is no "inherent" order of rows in a relational database.

You have to sort according to the contents of the row (either in SQL or in your program), or, if that is not possible, you have to make it possible by adding another column which stores the sort order you want.

In your case, it looks like you can order on a date column (but you need to store the date as such, not as a string, because that will just sort in a meaningless alphabetical way).

If the original array is not always guaranteed to be in a defined order, you may need to add the array index as an extra column -- maybe that is what ordering was for).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM