gpt4 book ai didi

mysql - 在 Yii 中将 SQL Server 查询转换为 MySQL

转载 作者:行者123 更新时间:2023-11-30 01:36:30 25 4
gpt4 key购买 nike

如何在 Yii 中使用 MySQL 编写此查询?

Yii::app()->db->createCommand()
->select("C.[id], C.[name], C.[family], C.[mobile]
, Stuff((
SELECT ', ' + (contact_group.name)
FROM contact_grouping AS CGG , contact_group
WHERE CGG.contact = C.Id AND CGG.[group] = contact_group.id

FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)'), 1, 2, '') AS [groups]")
->from("contact AS c")
->where("C.[user] = {$UID} AND C.[attr] = ".self::ATTR_NORMAL.$con)

最佳答案

您正在寻找GROUP_CONCAT 。您的查询可能类似于

Yii::app()->db->createCommand()
->select("c.id, c.name, c.family, c.mobile,
(SELECT GROUP_CONCAT(g.name)
FROM contact_grouping gg JOIN contact_group g
ON g.id = gg.`group`
WHERE gg.contact = c.id
GROUP BY gg.contact) groups")
->from("contact c")
->where("c.user = {$UID} AND c.attr = ".self::ATTR_NORMAL.$con)

未测试

关于mysql - 在 Yii 中将 SQL Server 查询转换为 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16807262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com