mysql - 选择一个 sql 字段并且不显示所选内容的特定单词

标签 mysql sql mariadb

当此查询返回“中心”寄存器时,所有同名字段中都有一个我不想显示的单词。我可以通过 PHP 实现,但我需要在数据库上使用它。

基础知识我都知道,但是好久没做过sql了

SELECT
o.id_order,
o.reference AS Ref,
c.firstname AS Name,
c.lastname AS Last Name,
pl.`name` AS Center,
od.product_name AS Product,
od.product_quantity AS Quant,
ROUND(od.product_price * 1.21,2) AS Price,
o.date_add AS `Date`
FROM ps_orders AS o
INNER JOIN ps_order_detail AS od ON od.id_order = o.id_order
INNER JOIN ps_customer AS c ON c.id_customer = o.id_customer
INNER JOIN ps_product_lang AS pl ON pl.id_product = od.product_id
WHERE pl.id_lang = 1
ORDER BY od.id_order_detail DESC

当这返回中心数据时,所有字段前面都有介词“The”,返回类似:

Center 
The Odoo Team Center
The Dev house

然后我需要展示类似的内容

Center 
Odoo Team Center
Dev house

最佳答案

您可以使用REPLACE:

    SELECT
    o.id_order,
    o.reference AS Ref,
    c.firstname AS Name,
    c.lastname AS Last Name,
    REPLACE(pl.`name`,"The ","") AS Center,
    od.product_name AS Product,
    od.product_quantity AS Quant,
    ROUND(od.product_price * 1.21,2) AS Price,
    o.date_add AS `Date`
    FROM ps_orders AS o
    INNER JOIN ps_order_detail AS od ON od.id_order = o.id_order
    INNER JOIN ps_customer AS c ON c.id_customer = o.id_customer
    INNER JOIN ps_product_lang AS pl ON pl.id_product = od.product_id
    WHERE pl.id_lang = 1
    ORDER BY od.id_order_detail DESC

Documentation: https://dev.mysql.com/doc/refman/5.7/en/replace.html

Demo: http://sqlfiddle.com/#!9/a5640/91

关于mysql - 选择一个 sql 字段并且不显示所选内容的特定单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49433397/

相关文章:

mysql - 多表SQL查询(涉及外键)

php - 如何使用 PHP 从数据库中检索某些表?

sql - 如何将 group by 与应始终包含的值一起使用?

mysql - 关联 3 个 SQL 实体的建议

python - 字符串值错误错误 - Python + mariaDB

php - 使用 docker 在 CentO 上设置 PHP-FPM、Nginx、Mariadb

mysql - 根据日期时间的准确度对表中的数据进行排序

php - 访问php中的JavaScript变量值以存储在mysql中

python - 将组合框值传递给 SQL WHERE 时出错

c++ - 如何以编程方式克隆 mysql 数据库