是否可以使用MySQL上传CSV文件的前10行 - LOAD DATA LOCAL INFILE?

时间:2013-02-19 22:46:46

标签: php mysql load local

是否可以使用MySQL上传CSV文件的前10行 - LOAD DATA LOCAL INFILE?我尝试使用LIMIT,但它不起作用。

这是我的PHP脚本:

    $sql = "LOAD DATA LOCAL INFILE '".@mysql_escape_string($this->file_name).
         "' INTO TABLE branches
              FIELDS TERMINATED BY ',' ENCLOSED BY '\"'
              LINES TERMINATED BY '\r\n'
              IGNORE 1 LINES                  
              (
                Name,
                Address_Line_1, 
                City, 
                State, 
                Country_Code,
                Postal_Code, 
                Main_Phone, 
                Google_Places_Link, 
                Custom_Directory_1, 
                Custom_Directory_2, 
                Custom_Directory_3,  
                business_id,
                username
              ) SET branches.business_id=(".$this->business_id."), branches.username=('".$this->username."') LIMIT 0,10  
              ";

2 个答案:

答案 0 :(得分:0)

最新的mysql文档https://dev.mysql.com/doc/refman/8.0/en/load-data.html

LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
    [REPLACE | IGNORE]
    INTO TABLE tbl_name
    [PARTITION (partition_name [, partition_name] ...)]
    [CHARACTER SET charset_name]
    [{FIELDS | COLUMNS}
        [TERMINATED BY 'string']
        [[OPTIONALLY] ENCLOSED BY 'char']
        [ESCAPED BY 'char']
    ]
    [LINES
        [STARTING BY 'string']
        [TERMINATED BY 'string']
    ]
    [IGNORE number {LINES | ROWS}]
    [(col_name_or_user_var
        [, col_name_or_user_var] ...)]
    [SET col_name={expr | DEFAULT},
        [, col_name={expr | DEFAULT}] ...]

没有任何方法/命令可以执行您想要的操作。

答案 1 :(得分:0)

以下解决方案为 function* fetchCarsSaga() { try { const {data} = yield call(fetchCarsService); // this action should store the data to the store // it needs the data as an argument! yield put(fetchCarsSuccess(data)); } catch(error) { yield put(fetchCarsFailure(error)); } } 提供了 LIMIT 风格的行为。使用 const Cars = () => { const dispatch = useDispatch(); const cars = useSelector( state => state.cars.data ) || []; const isLoading = useSelector( state => state.cars.isLoading ); const error = useSelector( state => state.cars.error ); useEffect( () => { dispatch(fetchCarsRequest()); // action that calls the saga }, [] ); return ( {!! error && (<div>Error: {error}</div>)} {isLoading ? <Spinner/> : ( <ul> {cars.map((car) => <li key={car.id}>{car.name}</li>)} </ul> )} ) } 并设置为总行数 - 任何您想要测试的小数

这不会节省文件扫描,不优雅,并且会向后工作(字面意思)。但是对于长度小于一百万行的中等长度的表,对我来说它对测试很有用。 (它可能适用于更大的表,但我没有机会进行测试。)

例如,一个有 101,773 行的大表的最后 20 行是

LOAD DATA INFILE