MySQL table multi-column sorting, get a specified value of the previous or next, how to do a simple point

Source: Internet
Author: User
MySQL table multi-column sort, get a specified value of the previous or next, how to do a simple point
A invoicing database MySQL, the table name Tblsale, there is a date and index two fields to determine a sales order (there is N single day, with index to distinguish), the table is not necessarily the record in order to discharge, because there is the possibility of the subsequent replenishment.
Simply sort the records in the table, you can use order by date, index to row (depending on the situation in ascending or descending order).
Now the front end passes a date and an index, based on the specific date and index to find its previous or next order. In PHP, how do you make it simple?

Share to: more


------Solution--------------------
References:
after reading the reply, I think I said something less in front of me.
Regarding the previous single, the next one, we have to consider an extreme situation, I above a single example to say:
In general, the so-called last single is the date of the same situation, index-1 (consider the full point, in case the index is inconsistent, take a smaller than the index), but, if the index is the smallest of the day? At this point, it is necessary to consider the date of the day before the election (if not, then the day before the election ...) ), and then, in the case of fixed date, select the largest of the index ...
The next one is a similar truth.
Trouble is here.

In fact, you have to add these, in fact, has abandoned the index of this condition.
Then you have to combine the program to achieve, the above a single example is as follows:


ORDER by index ASC LIMIT 1 ";
$result = mysql_query ($sql);
$row = Mysql_fetch_row ($result); Here is the normal situation, both values
if (empty ($row)) {////The so-called extreme case comes up: , a last order with a single date

ORDER by Date,index DESC LIMIT 1 ";
$result = mysql_query ($sql);
$row = Mysql_fetch_row ($result);
}
Print_r ($row);

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.