{"id":1894,"date":"2014-03-30T09:57:51","date_gmt":"2014-03-30T07:57:51","guid":{"rendered":"http:\/\/www.extradrm.com\/?p=1894"},"modified":"2014-03-30T10:21:25","modified_gmt":"2014-03-30T08:21:25","slug":"how-to-import-csv-file-to-mysql-table-via-phpmyadmin","status":"publish","type":"post","link":"http:\/\/www.extradrm.com\/?p=1894","title":{"rendered":"Import CSV file to MySQL table \/ CAST conversions"},"content":{"rendered":"<p>To import CSV unicode file to MySQL table via phpmyadmin (csv fields are delimited with tabulations ) :<\/p>\n<p><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/03\/csv-mysql.jpg\"><img loading=\"lazy\" alt=\"csv-mysql\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/03\/csv-mysql-300x160.jpg\" width=\"300\" height=\"160\" \/><\/a><\/p>\n<p><strong>More advanced options can be used at the shell level :<\/strong><\/p>\n<pre>shell&gt; <strong><code>cat \/tmp\/bit_test.txt<\/code><\/strong>\r\n2\r\n127\r\nshell&gt; <strong><code>mysql test<\/code><\/strong>\r\nmysql&gt; <strong><code>LOAD DATA INFILE '\/tmp\/bit_test.txt'<\/code><\/strong>\r\n    -&gt; <strong><code>INTO TABLE bit_test (@var1) SET b = CAST(@var1 AS UNSIGNED);<\/code><\/strong>\r\nQuery OK, 2 rows affected (0.00 sec)\r\nRecords: 2  Deleted: 0  Skipped: 0  Warnings: 0\r\n\r\nmysql&gt; <strong><code>SELECT BIN(b+0) FROM bit_test;<\/code><\/strong>\r\n+----------+\r\n| bin(b+0) |\r\n+----------+\r\n| 10       |\r\n| 1111111  |\r\n+----------+\r\n2 rows in set (0.00 sec)<\/pre>\n<p><strong>CAST and STR_TO_DATE are useful conversions calls while using mysql transactions :<\/strong><\/p>\n<pre>Sample 1 : STR_TO_DATE converting string (like : 12\/10\/1989) in real date for obtaining real sorting on dates :\r\nSELECT * FROM perio JOIN bulletinage ON bulletinage.per_ref = perio.per_ref \r\nWHERE perio.per_ref = 'PER00000001' \r\nORDER BY str_to_date( bulletinage.per_numero, '%d\/%m\/%Y' ) ASC;<\/pre>\n<hr\/>\n<pre>Sample 2 : CAST converting num strings in unsigned for obtaining integer order sorting :\r\nSELECT * FROM perio per,bulletinage expp \u00a0\r\nWHERE per.per_ref = expp.per_ref and per.per_ref= 'per00000003' order by CAST(expp.isn As UNSIGNED)<\/pre>\n<p>To use STR_TO_DATE With codeIgniter we can use :<\/p>\n<pre> public function get_expp_by_id($id)\r\n    {\r\n        $sql=\"SELECT * FROM perio JOIN bulletinage ON bulletinage.per_ref = perio.per_ref \r\n        WHERE perio.per_ref = '\" . $id . \"' ORDER BY str_to_date( bulletinage.per_numero, '%d\/%m\/%Y' ) ASC;\";\r\n        \r\n        $query = $this->db->query($sql);\r\n       return $query;\r\n }    <\/pre>\n<p>When Conversion do not pose escape problems in codeIgniter like CAST we can use Active record like that :<\/p>\n<pre>\r\npublic function get_expp1_by_id($id)\r\n    {\r\n        $this->db->select('*');\r\n        $this->db->from('perio');\r\n        $this->db->join('bulletinage', 'bulletinage.per_ref = perio.per_ref');\r\n        $this->db->where('perio.per_ref', $id);\r\n        $this->db->order_by(\"CAST(bulletinage.isn As UNSIGNED)\", \"asc\"); \r\n        $query = $this->db->get();\r\n\treturn $query;   \r\n    }    <\/pre>\n","protected":false},"excerpt":{"rendered":"<p>To import CSV unicode file to MySQL table via phpmyadmin (csv fields are delimited with tabulations ) : More advanced options can be used at the shell level : shell&gt; cat \/tmp\/bit_test.txt 2 127&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":2841,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,35],"tags":[],"youtube_video":null,"_links":{"self":[{"href":"http:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/1894"}],"collection":[{"href":"http:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1894"}],"version-history":[{"count":0,"href":"http:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/1894\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/media\/2841"}],"wp:attachment":[{"href":"http:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1894"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}