导入SQL的CSV数据以错误的顺序显示数据

时间:2014-07-05 18:08:31

标签: sql excel csv import

我目前正在在线大学服务器上为sql / phpmadmin开发后端数据库系统。

我希望将数据存储在各个表中,以便客户:

  1. 可以搜索查看可用的航班和航班调度员
  2. 将能够查看表格中的数据,并指定工作人员在特定日期内的某些航班。
  3. 我一直在使用csv文件上传来自excel的数据,其中行是列在phpmyadmin上,而不是在excel文件上。

    所有文件都正确上传,除了一个缺陷 - 我按顺序订购航班代码但是当我上传这些或某些其他文件时,它会以错误的顺序上传它们。我想顺序1-18;相反,它是1,10,11,然后是2-9等。

    上传时,我选择了csv类型,并被告知将“with contained with”字段留空,并勾选“替换现有数据”框。

    有谁知道如何解决这个问题?我不希望它影响我如何执行此数据库的查询。

    如果需要,我可以包含表格输出。如果我在上传后点击编辑,代码会显示正确的顺序,如下所示:

    REPLACE INTO `routes` VALUES ('1', 'FWL-101', 'BFS', 'LGW', '600', '710')# 1 row affected.
    REPLACE INTO `routes` VALUES ('2', 'FWL-102', 'LGW', 'BFS', '700', '810')# 1 row affected.
    REPLACE INTO `routes` VALUES ('3', 'FWL-103', 'BFS', 'LGW', '800', '910')# 1 row affected.
    REPLACE INTO `routes` VALUES ('4', 'FWL-104', 'LGW', 'BFS', '900', '1010')# 1 row affected.
    REPLACE INTO `routes` VALUES ('5', 'FWL-105', 'BFS', 'LGW', '1000', '1110')# 1 row affected.
    REPLACE INTO `routes` VALUES ('6', 'FWL-106', 'LGW', 'BFS', '1100', '1210')# 1 row affected.
    REPLACE INTO `routes` VALUES ('7', 'FWL-107', 'BFS', 'BRU', '700', '915')# 1 row affected.
    REPLACE INTO `routes` VALUES ('8', 'FWL-108', 'BRU', 'BFS', '800', '1015')# 1 row affected.
    REPLACE INTO `routes` VALUES ('9', 'FWL-109', 'BFS', 'HAM', '600', '840')# 1 row affected.
    REPLACE INTO `routes` VALUES ('10', 'FWL-110', 'HAM', 'BFS', '700', '940')# 1 row affected.
    REPLACE INTO `routes` VALUES ('11', 'FWL-111', 'BFS', 'GNB', '1000', '1215')# 1 row affected.
    REPLACE INTO `routes` VALUES ('12', 'FWL-112', 'GNB', 'BFS', '1100', '1415')# 1 row affected.
    REPLACE INTO `routes` VALUES ('13', 'FWL-113', 'LGW', 'BRU', '700', '905')# 1 row affected.
    REPLACE INTO `routes` VALUES ('14', 'FWL-114', 'BRU', 'LGW', '800', '1005')# 1 row affected.
    REPLACE INTO `routes` VALUES ('15', 'FWL-115', 'LGW', 'HAM', '600', '850')# 1 row affected.
    REPLACE INTO `routes` VALUES ('16', 'FWL-116', 'HAM', 'LGW', '700', '950')# 1 row affected.
    REPLACE INTO `routes` VALUES ('17', 'FWL-117', 'LGW', 'GRB', '1000', '1200')# 1 row affected.
    REPLACE INTO `routes` VALUES ('18', 'FWL-118', 'GRB', 'LGW', '1100', '1400')# 1 row affected.
    

    但是,当我上传csv文件后刷新数据库时,它显示如下:

    战斗代码以FWL开头,并且应按升序数字顺序显示,以及旁边显示的时间。这是以下不正确的顺序:

    Edit Edit
     Copy Copy
     Delete Delete
    1
    FWL-101
    BFS
    LGW
    600
    710
    
    Edit Edit
     Copy Copy
     Delete Delete
    10
    FWL-110
    HAM
    BFS
    700
    940
    
    Edit Edit
     Copy Copy
     Delete Delete
    11
    FWL-111
    BFS
    GNB
    1000
    1215
    
    Edit Edit
     Copy Copy
     Delete Delete
    12
    FWL-112
    GNB
    BFS
    1100
    1415
    
    Edit Edit
     Copy Copy
     Delete Delete
    13
    FWL-113
    LGW
    BRU
    700
    905
    
    Edit Edit
     Copy Copy
     Delete Delete
    14
    FWL-114
    BRU
    LGW
    800
    1005
    
    Edit Edit
     Copy Copy
     Delete Delete
    15
    FWL-115
    LGW
    HAM
    600
    850
    
    Edit Edit
     Copy Copy
     Delete Delete
    16
    FWL-116
    HAM
    LGW
    700
    950
    
    Edit Edit
     Copy Copy
     Delete Delete
    17
    FWL-117
    LGW
    GRB
    1000
    1200
    
    Edit Edit
     Copy Copy
     Delete Delete
    18
    FWL-118
    GRB
    LGW
    1100
    1400
    
    Edit Edit
     Copy Copy
     Delete Delete
    2
    FWL-102
    LGW
    BFS
    700
    810
    
    Edit Edit
     Copy Copy
     Delete Delete
    3
    FWL-103
    BFS
    LGW
    800
    910
    
    Edit Edit
     Copy Copy
     Delete Delete
    4
    FWL-104
    LGW
    BFS
    900
    1010
    
    Edit Edit
     Copy Copy
     Delete Delete
    5
    FWL-105
    BFS
    LGW
    1000
    1110
    
    Edit Edit
     Copy Copy
     Delete Delete
    6
    FWL-106
    LGW
    BFS
    1100
    1210
    
    Edit Edit
     Copy Copy
     Delete Delete
    7
    FWL-107
    BFS
    BRU
    700
    915
    
    Edit Edit
     Copy Copy
     Delete Delete
    8
    FWL-108
    BRU
    BFS
    800
    1015
    
    Edit Edit
     Copy Copy
     Delete Delete
    9
    FWL-109
    BFS
    HAM
    600
    840
    

1 个答案:

答案 0 :(得分:0)

在varchar字段上使用ORDER BY会使值“12”在值“2”之前显示 - 如果排序字符串,这是正确的。在您的数据库中,或者至少在您的订单中,您应该执行以下操作:

ORDER BY cast(<my_varchar_field_of_numeric_values> as int)

您还需要确保可能正确处理可能显示在该列中的任何非数字值( 一个varchar字段,因此您永远不知道)。