简体   繁体   English

为什么我的值以升序保存在数据库中?

[英]Why are my values being saved in ascending order in the database?

I've just noticed that my rand values are being saved in ascending order (lower to higher), but I'm not telling it to save it in ascending order. 我刚刚注意到我的rand值是按升序(从低到高)保存的,但是我没有告诉它要按升序保存。 However when I echo the rand, it shows the value in normal way. 但是,当我回显兰特时,它以正常方式显示值。

Source code: 源代码:

<?php
try {
  $db = new PDO('sqlite:randvalue.sqlite');
  $db->exec("CREATE TABLE company (revenue_value INTEGER PRIMARY KEY, month VARCHAR)");

  $i=1;
  while($i <= 12) {
    $randNum =  rand(1,100);
    $finalval = $randNum;
    $stmt = $db->prepare('INSERT INTO company (revenue_value) VALUES (?)');
    $stmt->execute(array("$finalval"));
    echo "$finalval";
    echo "<br>";
    $i++;
  };

  print "<table border=1>";
  print "<tr><td>value</td>";
  $result = $db->query('SELECT * FROM company');

  foreach($result as $row) {
    print "<tr><td>".$row['revenue_value']."</td>";
  }
  print "</table>";

  $db = NULL;

} catch(PDOException $e) {
  print 'Exception : '.$e->getMessage();
}
?>

Result: 结果:

在此处输入图片说明

How do I make it not to save the value in table in non-ascending order just like in echo where the values are not in ascending order? 如何避免不按升序将值保存在表中,就像回声中值不按升序保存一样?

The database will attempt to store the rows in the most efficient way it can for fast lookup. 数据库将尝试以最有效的方式来存储行,以便快速查找。 This means storing them in the order defined by the primary key of the table, if one exists, and in this case, the revenue_value column is the primary key. 这意味着按表的主键定义的顺序存储它们(如果存在的话),在这种情况下, revenue_value列是主键。 There's no guarantee that the order in which records are inserted is the order in which they will come back when you do a SELECT . 不能保证插入记录的顺序就是执行SELECT时记录返回的顺序。

If you want to pull the back in the records same order, you'll need a separate column to store the order in which they are inserted. 如果要按相同顺序向后拉记录,则需要单独的一列来存储插入记录的顺序。 Typically, you'd use a AUTO_INCREMENT column that's also the primary key of the table. 通常,您将使用AUTO_INCREMENT列,该列也是表的主键。 Add a column like this and you'll be able to pull them back in the order in which they are inserted by using an ORDER BY clause. 添加一个这样的列,您将可以使用ORDER BY子句按插入顺序将它们拉回。 However, as I said, the database will usually attempt to store the rows efficiently, it order them by the primary key column anyway, so typically you wouldn't need the ORDER BY , but it's still a good idea to include one in any query where the order of the results is important. 但是,正如我所说,数据库通常会尝试高效地存储行,无论如何都要通过主键列对行进行排序,因此通常不需要 ORDER BY ,但是在任何查询中都包含一个行仍然是一个好主意结果顺序很重要的地方。

thanks to @Rizier manage to find the problem 感谢@Rizier设法找到问题

just remove the primary key 只需删除主键

$db->exec("CREATE TABLE company (revenue_value INTEGER PRIMARY KEY, month VARCHAR)");

to

  $db->exec("CREATE TABLE company (revenue_value INTEGER, month VARCHAR)");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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