sql >> Database teknologi >  >> RDS >> Mysql

Sådan gemmer du manglende FLOAT-værdier med NULL i SQL

Problemet er, hvordan MySQL fortolker et tomt felt kontra et manglende felt . Fra dokumenterne til LOAD DATA INFILE ...

I dette tilfælde ser det ud til, at MySQL anser det for at være tomt . Du kan se dette fra show warnings .

mysql> show warnings;
+---------+------+------------------------------------------------------------+
| Level   | Code | Message                                                    |
+---------+------+------------------------------------------------------------+
| Warning | 1265 | Data truncated for column 'customerReviewAverage' at row 2 |
| Warning | 1265 | Data truncated for column 'customerReviewAverage' at row 3 |
+---------+------+------------------------------------------------------------+
2 rows in set (0.00 sec)

Hvorimod hvis vi fjerner de efterfølgende kommaer, så feltet mangler ...

19658847,Glanzlichter - CD,,12.99,5.0
19658856,Glanzlichter - CD,,6.99
19658865,Glanzlichter - CD,,8.99
1965886,Beach Boys '69 - CASSETTE,,6.99,4.5

Derefter sættes dataene til null.

mysql> show warnings;
+---------+------+--------------------------------------------+
| Level   | Code | Message                                    |
+---------+------+--------------------------------------------+
| Warning | 1261 | Row 2 doesn't contain data for all columns |
| Warning | 1261 | Row 3 doesn't contain data for all columns |
+---------+------+--------------------------------------------+

mysql> select * from products;   
+----------+---------------------------+-------------+--------------+-----------------------+
| sku      | name                      | description | regularPrice | customerReviewAverage |
+----------+---------------------------+-------------+--------------+-----------------------+
| 19658847 | Glanzlichter - CD         | NULL        |        12.99 |                     5 |
| 19658856 | Glanzlichter - CD         | NULL        |         6.99 |                  NULL |
| 19658865 | Glanzlichter - CD         | NULL        |         8.99 |                  NULL |
|  1965886 | Beach Boys '69 - CASSETTE | NULL        |         6.99 |                   4.5 |
+----------+---------------------------+-------------+--------------+-----------------------+
4 rows in set (0.00 sec)

Gør det samme for @customerReviewAverage som for @description fungerede for mig i MySQL 5.7.

LOAD DATA
  LOCAL INFILE 'product.csv'
  INTO TABLE Products
  FIELDS TERMINATED BY ','
  ENCLOSED BY '"'
  LINES TERMINATED BY '\n'
  (sku, name, @description, regularPrice, @customerReviewAverage)       
  SET description           = IF(@description='',NULL,@description),
      customerReviewAverage = IF(@customerReviewAverage='',NULL,@customerReviewAverage);

mysql> select * from products;
+----------+---------------------------+-------------+--------------+-----------------------+
| sku      | name                      | description | regularPrice | customerReviewAverage |
+----------+---------------------------+-------------+--------------+-----------------------+
| 19658847 | Glanzlichter - CD         | NULL        |        12.99 |                     5 |
| 19658856 | Glanzlichter - CD         | NULL        |         6.99 |                  NULL |
| 19658865 | Glanzlichter - CD         | NULL        |         8.99 |                  NULL |
|  1965886 | Beach Boys '69 - CASSETTE | NULL        |         6.99 |                   4.5 |
+----------+---------------------------+-------------+--------------+-----------------------+
4 rows in set (0.00 sec)



  1. Kan jeg køre en sql-fil på min Google Cloud SQL-instans ved hjælp af kommandoen gcloud sql

  2. hvorfor får jeg en syntaksfejl for forberedt erklæring?

  3. unikt id i mere end én tabel [MySQL]

  4. hvordan man undgår dødvande i mysql