I have a MySQL table where there are two fields that make up the primary key, and a "sort" field. None of these are auto-incrementing.
When you do a query on the table without specifying an ORDER BY
clause, it pulls the data out in the order that it was put in -- pretty standard. So whichever rows were inserted first, it pulls those out first.
The problem is that when I ORDER BY sort ASC
, if sort
is null, then it really doesn't sort at all and the results just become jumbled up. But I want to have the backup ORDER BY
be that default order that it would otherwise pull out of. I can't have the backup be an auto-incrementing "ID" field, because there is no auto-incrementing ID field. The primary key is just a combination of two foreign keys.
-----EDIT-----
Table:
CREATE TABLE IF NOT EXISTS `product_attribute_select_value` ( `product_attribute_id` int(11) NOT NULL, `attribute_select_value_id` int(11) NOT NULL, `sort` int(11) default '0', PRIMARY KEY (`product_attribute_id`,`attribute_select_value_id`), KEY `product_attribute_select_value_FI_2` (`attribute_select_value_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; ALTER TABLE `product_attribute_select_value` ADD CONSTRAINT `product_attribute_select_value_FK_1` FOREIGN KEY (`product_attribute_id`) REFERENCES `product_attribute` (`id`) ON DELETE CASCADE, ADD CONSTRAINT `product_attribute_select_value_FK_2` FOREIGN KEY (`attribute_select_value_id`) REFERENCES `attribute_select_value` (`id`);
Data:
The data is inserted, and when pulled out without an ORDER BY
clause, it comes out in the order it was put in. When there is an ORDER BY
clause and all of the sort
values are 0
, it pulls it out in a seemingly random order.