【问题标题】:Need to trim new lines from MySql result需要从 MySql 结果中修剪新行
【发布时间】:2016-04-28 17:40:33
【问题描述】:

我正在使用 MySql 工作台从 Magento 数据库备份数据。这是我正在使用的查询:

Select FName.value AS 'FirstName' ,
LName.value AS 'LastName' ,
e.email,
Address1.value AS Address1, 
City.value AS City ,
Province.value as Province ,
Country.value AS 'CountryCode' ,
Zip.value AS Zip ,
Phone.value AS Phone
FROM customer_entity e 
JOIN customer_address_entity ae ON ae.parent_id = e.entity_id 
JOIN customer_entity_int cei ON e.entity_id = cei.entity_id and cei.attribute_id=13 and cei.value=ae.entity_id 
left JOIN customer_address_entity_varchar FName ON ae.entity_id = FName.entity_id        and FName.attribute_id = (select attribute_id from eav_attribute where entity_type_id=2 and attribute_code='firstname')
left JOIN customer_address_entity_varchar LName ON ae.entity_id = LName.entity_id         and LName.attribute_id= (select attribute_id from eav_attribute where entity_type_id=2 and attribute_code='lastname')
left JOIN customer_address_entity_varchar Zip ON ae.entity_id = Zip.entity_id                         and Zip.attribute_id = (select attribute_id from eav_attribute where entity_type_id=2 and attribute_code='postcode')
left JOIN customer_address_entity_varchar City ON ae.entity_id = City.entity_id                      and City.attribute_id= (select attribute_id from eav_attribute where entity_type_id=2 and attribute_code='city')
left JOIN customer_address_entity_varchar Phone ON ae.entity_id = Phone.entity_id           and Phone.attribute_id = (select attribute_id from eav_attribute where entity_type_id=2 and attribute_code='telephone') 
left JOIN customer_address_entity_varchar Province ON ae.entity_id = Province.entity_id and Province.attribute_id = (select attribute_id from eav_attribute where entity_type_id=2 and attribute_code='region')
left JOIN customer_address_entity_varchar Country ON ae.entity_id = Country.entity_id    and Country.attribute_id = (select attribute_id from eav_attribute where entity_type_id=2 and attribute_code='country_id')
left JOIN customer_address_entity_text Address1 ON ae.entity_id = Address1.entity_id     and Address1.attribute_id = (select attribute_id from eav_attribute where entity_type_id=2 and attribute_code='street')

我真正需要做的就是从 Address1 字段中删除新行,因为它破坏了我使用的 CSV 解析器。我试过了:

TRIM(\n FROM Address1.value) as Address1

在查询的第四行,但没有成功。我对 MySql 的了解显然是有限的,任何帮助都会为我节省数小时删除新行的时间。

【问题讨论】:

    标签: mysql magento csv newline trim


    【解决方案1】:

    您可以通过运行从Address1.value 字段中删除所有“\n”

    UPDATE customer_address_entity_text SET value=REPLACE(value,'\n','')
    

    然后运行您的查询。

    另一种解决方案是在您的 SELECT 语句中使用 REPLACE()

    REPLACE(Address1.value, '\n', '') as Address1
    

    导致

    Select FName.value AS 'FirstName' ,
    LName.value AS 'LastName' ,
    e.email,
    REPLACE(Address1.value, '\n', '') AS Address1, 
    City.value AS City ,
    Province.value as Province ,
    Country.value AS 'CountryCode' ,
    Zip.value AS Zip ,
    Phone.value AS Phone
    FROM customer_entity e 
    JOIN customer_address_entity ae ON ae.parent_id = e.entity_id 
    JOIN customer_entity_int cei ON e.entity_id = cei.entity_id and cei.attribute_id=13 and cei.value=ae.entity_id 
    left JOIN customer_address_entity_varchar FName ON ae.entity_id = FName.entity_id        and FName.attribute_id = (select attribute_id from eav_attribute where entity_type_id=2 and attribute_code='firstname')
    left JOIN customer_address_entity_varchar LName ON ae.entity_id = LName.entity_id         and LName.attribute_id= (select attribute_id from eav_attribute where entity_type_id=2 and attribute_code='lastname')
    left JOIN customer_address_entity_varchar Zip ON ae.entity_id = Zip.entity_id                         and Zip.attribute_id = (select attribute_id from eav_attribute where entity_type_id=2 and attribute_code='postcode')
    left JOIN customer_address_entity_varchar City ON ae.entity_id = City.entity_id                      and City.attribute_id= (select attribute_id from eav_attribute where entity_type_id=2 and attribute_code='city')
    left JOIN customer_address_entity_varchar Phone ON ae.entity_id = Phone.entity_id           and Phone.attribute_id = (select attribute_id from eav_attribute where entity_type_id=2 and attribute_code='telephone') 
    left JOIN customer_address_entity_varchar Province ON ae.entity_id = Province.entity_id and Province.attribute_id = (select attribute_id from eav_attribute where entity_type_id=2 and attribute_code='region')
    left JOIN customer_address_entity_varchar Country ON ae.entity_id = Country.entity_id    and Country.attribute_id = (select attribute_id from eav_attribute where entity_type_id=2 and attribute_code='country_id')
    left JOIN customer_address_entity_text Address1 ON ae.entity_id = Address1.entity_id     and Address1.attribute_id = (select attribute_id from eav_attribute where entity_type_id=2 and attribute_code='street')
    

    【讨论】:

      【解决方案2】:

      选择不同的路线。

      摆脱装饰和东西

      TRIM(\n FROM Address1.value) as Address1
      

      应该是

      table.address1 as Address1
      

      然后在所有连接之后添加一个 WHERE 子句并将其设置为您认为新的日期。

      应该是这样的:

      WHERE date =< 2016-04-28
      

      这应该使它只显示日期列早于今天日期的行。

      哦,如果您实际上不想使用日期列,但您知道新行开始的 ID # 您也可以随时使用它。它应该是相同的语法但具有不同的列

      希望对您有所帮助。如果您需要更多帮助,请告诉我

      另外需要注意的是,建议使用小写的表名和列名。所以我也会对这些进行一些编辑,并确保所有内容都是小写,因为 SQL 识别大写

      【讨论】:

      • 我需要所有这些,所以我不想用 WHERE 子句限制日期。我需要一种方法来确保 Address1.value 中没有任何换行符(\n)。
      • 试一试怎么样: where address1.value NOT LIKE '/n' 应该收集子字符串 /n 并且不允许它。这就是您要寻找的更多内容吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-23
      • 2012-11-29
      • 2023-03-03
      • 1970-01-01
      • 2016-07-13
      • 1970-01-01
      • 2016-07-28
      相关资源
      最近更新 更多