mysql concat on列名

nwwlzxa7  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(399)

嗨,我刚刚开始学习sql,我希望使用concat将这些列合并成一个列,但没有做到这一点。我可以在没有concat的情况下运行代码,但是当我使用concat时,它会给我一个错误代码。有人能告诉我我做错了什么吗?

SELECT CONCAT('A purchase with the purchase ID of' AS "Constraint",
ONLINEPURCHASE.PurchaseID AS "OLID", 'is an online purchase of type' AS "Condition", ONLINEPURCHASE.OnlineType AS "OLType", 'and also a walkin purchase of location' AS "Condition", WALKINPURCHASE.ShopLocation AS "ShopLocation") 
FROM ONLINEPURCHASE JOIN WALKINPURCHASE
    ON ONLINEPURCHASE.PurchaseID = WALKINPURCHASE.PurchaseID
WHERE WALKINPURCHASE.PurchaseID IN (SELECT PurchaseID
        FROM WALKINPURCHASE);

但是出现了这个错误(错误1583(42000):对本机函数'concat'的调用中的参数不正确)

u7up0aaq

u7up0aaq1#

恭喜你开始学习sql,它非常有用!让我设法消除你的误解。
这个 CONCAT 函数接受一定数量的字符串或列值并将它们连接在一起。没有必要使用 AS 关键字。
我想你要找的是:

SELECT
  CONCAT(
    'A purchase with the purchase ID of',
    ONLINEPURCHASE.PurchaseID,
    'is an online purchase of type',
    ONLINEPURCHASE.OnlineType,
    'and also a walkin purchase of location',
    WALKINPURCHASE.ShopLocation
  ) AS result
FROM ONLINEPURCHASE JOIN WALKINPURCHASE
  ON ONLINEPURCHASE.PurchaseID = WALKINPURCHASE.PurchaseID
WHERE WALKINPURCHASE.PurchaseID IN
  (SELECT PurchaseID FROM WALKINPURCHASE);

这将创建你正在构建的句子 CONCAT 语句,并将其作为结果集的一列返回(我用过 AS 关键字重命名已生成的列。)
如果你对此有任何疑问,请告诉我。

相关问题