Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomatic sequencing of records in MySQL
    primarykey
    data
    text
    <p>I have a MySQL table containing telephone numbers belonging to companies and individuals (customers). Each customer can have more than one telephone number, and they often want them listed in a particular order, so the table has a <code>sequence</code> field to sort each customer's telephone numbers according to their preference. The fields are:</p> <ul> <li><code>parent_id</code> - char</li> <li><code>sequence</code> - tinyint</li> <li><code>area_code</code> - string</li> <li><code>number</code> - string</li> </ul> <p>The primary key is the combination of <code>parent_id</code> and <code>sequence</code>.</p> <p>So far so good. The "problem" comes when I want to move a number up in the order, or delete a number.</p> <p>When moving a number up, the way I do it (with PHP) is changing that record's <code>sequence</code> to 0 (a customer's first telephone number is normally 1, I use 0 just for this), then incrementing the preceding number's <code>sequence</code> by 1, and then setting the original record's <code>sequence</code> to 1 less than it was. In other words, swapping the <code>sequence</code> with the preceding telephone number, but it takes three queries.</p> <p>When deleting a telephone number, I delete that record, and then I have to re-sort that customer's remaining telephone numbers so that there is no missing <code>sequence</code>. It depends on how many telephone numbers that customer has, but it can be several queries.</p> <p>I was wondering if there is a more efficient way of doing it.</p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload