[英]No SQL Query results after successful CSV import in mysql using LOAD DATA LOCAL INFILE
在 mysql db 中成功導入 a.csv 文件的內容后,我遇到了一個奇怪的問題。 來自 csv 文件的數據已成功導入到數據庫表中,但如果我在表上運行任何帶有條件的 SQL 查詢,則不會返回任何查詢結果。 我能夠運行查詢:
select * from mst_question
但如果指定條件並且滿足條件則不返回任何結果
select * from mst_question where qtype='single'
該表有一些行,其中 qtype 列包含條件文本“single”,但未返回任何結果。
奇怪的是,如果我編輯表中的“qtype”列內容並通過鍵入“single”替換測試“single”,則返回該行......對於我編輯的每一行!
我的.csv文件:
que_id,test_id,que_desc,ans1,ans2,ans3,ans4,true_ans,qtype
,11,In which year is the HTML specification supposed to be complete and finalized?,2012,2015,2020,2022,D,single
,11,Which of the following doctypes was introduced by HTML5?,<!doctype xhtml>,<!doctype html>,"<!doctype html PUBLIC ""-//W3C//DTD HTML 5.0 Transitional//EN"">","<!doctype html5 PUBLIC ""-//W3C//DTD HTML 5.0 Transitional//EN"">",B,single
,11,How do you stop crawlers from following links to sites you don't want to be associated with?,"<a href=""#"" rel=""nofollow""> ","<a href=""#"" rel=""dontgo""> ","<a href=""#"" rel=""nogo""> ","<a href=""#"" rel=""noassociation"">",A,single
,11,Which tag is used to define a section of the page that has content that is related but not critical to the main content in HTML5?,<article> ,<sidesection> ,<aside> ,<section> ,C,single
,11,The <article> is used to contain a main article. What is the tag used to break it into sections?,<article> ,<time> ,<aside> ,<section> ,D,single
我的 LOAD DATA LOCAL INFILE 語法:
LOAD DATA LOCAL INFILE 'quest.csv' INTO TABLE mst_question FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" IGNORE 1 LINES
Output 執行LOAD DATA LOCAL INFILE后:
(5 row(s)affected)
(0 ms taken)
我的 SQL 查詢(給出結果):
select * from mst_question
結果:
(5 row(s)returned)
(0 ms taken)
我的 SQL 條件簡單的查詢(沒有結果):
select * from mst_question where qtype='single'
結果:
(0 row(s)returned)
(0 ms taken)
我究竟做錯了什么????
找不到。。。請指教。。。
我的猜測是您的文件有 Windows 換行符:
...0,2022,D,single\r\n
您尚未指定LINES TERMINATED BY '\r\n'
子句,因此 MySQL 可能默認為 Unix 樣式( \n
),因此它實際上將single\r
導入到您的列中。
您可以使用HEX()
檢查確切的列內容。
在文本編輯器中打開 SQL 文件,然后查看每個插入行的末尾。 例子:
INSERT INTO `student` (`number`, `name`, `color`) VALUES
('1', 'David', 'Blue\r'),
('2', 'James', 'Yellow\r'),
請參閱 \r 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.