“连接时连接”的 SQL 查询

发布于 2024-09-19 11:11:16 字数 1371 浏览 7 评论 0原文

我正在使用 Sybase ASE 数据库。
我有两个表,如下所示:

Shops

---------------------
| ShopName | ShopID |
---------------------
| Sweetie  | 1      |
| Candie   | 2      |
| Sugarie  | 3      |
---------------------

Sweets

----------------------
| SweetName | ShopID |
----------------------
| lolly     | 1      |
| redlolly  | 1      |
| greenloly | 1      |
| taffy     | 2      |
| redtaffy  | 2      |
| bluetaffy | 2      |
| choco     | 3      |
| mintchoco | 3      |
| milkchoco | 3      |
| gummybees | 3      |
----------------------

我想编写一个查询来生成如下所示的结果:

-----------------------------------------------------
| ShopName | Sweets                                 |
-----------------------------------------------------
| Sweetie  | lolly, redlolly, greenlolly            |
| Candie   | taffy, redtaffy, bluetaffy             |
| Sugarie  | choco, mintchoco, milkchoco, gummybees |
-----------------------------------------------------

我应该如何去做?我需要这个用于 Sybase ASE 数据库。我尝试了 LIST() 函数,但出现错误。我查看了它的文档,结果发现,ASE版本中没有这个功能。

这可能意味着将涉及一些“动态sql”(我很少知道这意味着什么)。有人可以帮忙吗?

我可能想要结果表中的 ShopId 而不是 ShopName...我还不确定。我想这不会有太大区别。此外,结果 Sweets 列中的尾随逗号也不是问题。我想要的只是一个非空白分隔符。

I'm using a Sybase ASE database.
I have two tables that look like:

Table Shops:

---------------------
| ShopName | ShopID |
---------------------
| Sweetie  | 1      |
| Candie   | 2      |
| Sugarie  | 3      |
---------------------

Table Sweets:

----------------------
| SweetName | ShopID |
----------------------
| lolly     | 1      |
| redlolly  | 1      |
| greenloly | 1      |
| taffy     | 2      |
| redtaffy  | 2      |
| bluetaffy | 2      |
| choco     | 3      |
| mintchoco | 3      |
| milkchoco | 3      |
| gummybees | 3      |
----------------------

I want to write a query that would generate a result that looks like:

-----------------------------------------------------
| ShopName | Sweets                                 |
-----------------------------------------------------
| Sweetie  | lolly, redlolly, greenlolly            |
| Candie   | taffy, redtaffy, bluetaffy             |
| Sugarie  | choco, mintchoco, milkchoco, gummybees |
-----------------------------------------------------

How should I go about doing that? I need this for a Sybase ASE database. I tried the LIST() function, but I'm getting an error on that. I checked its documentation, and turns out, this function is not available in the ASE Edition.

This probably means that there will be some "dynamic sql" involved (I have very little idea what that means). Can anyone help?

I could want ShopId instead of ShopName in the results table... I don't know for sure yet. I guess that won't be much of a difference. Also, trailing commas in Sweets column of results is not an issue. All I want is a non-whitespace separator.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一瞬间的火花 2024-09-26 11:11:16

您必须指定您正在使用的 DBMS。

MySQL 的 GROUP CONCAT 是正是您所需要的。

SELECT ShopName, GROUP_CONCAT(SweetName SEPARATOR ", ")
FROM Shops a
JOIN Sweets b
ON a.ShopID = b.ShopID
GROUP BY ShopName

You'll have to specify what DBMS you're using.

MySQL's GROUP CONCAT is exactly what you need.

SELECT ShopName, GROUP_CONCAT(SweetName SEPARATOR ", ")
FROM Shops a
JOIN Sweets b
ON a.ShopID = b.ShopID
GROUP BY ShopName
迷乱花海 2024-09-26 11:11:16

这是一种交叉表查询,不可能在一个查询中使用 Sybase ASE。

您可以使用临时表创建一个存储过程,用游标填充它,然后从该临时表中进行选择。

It is a crosstab query and it is impossible with Sybase ASE in one query.

You can create a stored procedure with temporary table, fill it with cursor, and select from this temporary table.

神回复 2024-09-26 11:11:16

我在 SQL Server 上对此进行了测试,但希望它也适用于 Sybase。如果没有,也许它会让你足够接近解决它。

如果我创建这个函数:

CREATE FUNCTION SweetsList(@shopID int)
RETURNS varchar(500)
AS
BEGIN

    DECLARE @list varchar(500)

    SELECT @list = COALESCE(@list+', ','') + SweetName
    FROM Sweets
    WHERE ShopID = @shopID

    RETURN @list
END

然后我可以执行这个查询并获得您想要的结果:

SELECT ShopName, dbo.SweetsList(ShopID) AS Sweets
FROM Shops

希望这会有所帮助。

I tested this on SQL Server, but hopefully it will also work on Sybase. If not, maybe it will get you near enough to solve it.

If I create this function:

CREATE FUNCTION SweetsList(@shopID int)
RETURNS varchar(500)
AS
BEGIN

    DECLARE @list varchar(500)

    SELECT @list = COALESCE(@list+', ','') + SweetName
    FROM Sweets
    WHERE ShopID = @shopID

    RETURN @list
END

I can then execute this query and get the results you want:

SELECT ShopName, dbo.SweetsList(ShopID) AS Sweets
FROM Shops

Hope this helps.

娇柔作态 2024-09-26 11:11:16

不幸的是,adrift的答案中的方法不适用于Sybase ASE的select语句,变量@list不会更新每行,它仅适用于最后一行。但是因为更新对每一行执行并且表的大小不会很大,所以您可以使用更新语句来完成。小例子:

    declare @list varchar(500)

    update Sweets
    set @list = @list + SweetName + ', ' 
    where ShopID = 1

    select SUBSTRING(@list, 1, Len(@list) - 2)

PS 对于我来说,光标不是好方法......

Unfortunately, a method in adrift's answer don't work with select statement for Sybase ASE, variable @list don't update for each row, it works only for last row. But because update perform for each row and table's size don't to large, you could do it with update statement. Small example:

    declare @list varchar(500)

    update Sweets
    set @list = @list + SweetName + ', ' 
    where ShopID = 1

    select SUBSTRING(@list, 1, Len(@list) - 2)

P.S. As for me, cursor isn't good way...

伴我老 2024-09-26 11:11:16

在 Sybase ASE 中工作...

CREATE FUNCTION SweetsList(@SN varchar(10))
returns varchar(255)
AS
DECLARE @SwNList varchar(255)
DECLARE @FetchSwN varchar(55)
DECLARE @Status INT, @Error INT

DECLARE ListCurs CURSOR FOR
SELECT SweetName
  FROM Sweets AS SW
 JOIN Shops AS SH
  ON SH.ShopID = SW.ShopID
 WHERE SH.ShopName = @SN
FOR READ ONLY

OPEN ListCurs
SELECT @Status = 0
WHILE @Status = 0
BEGIN
    FETCH ListCurs INTO @FetchSwN

    SELECT @Status = @@SQLSTATUS

    IF @Status = 0 
    BEGIN
       SELECT  @SwNList = CASE WHEN @SwNList IS NULL THEN '' ELSE @SwNList + ', ' END + @FetchSwN
    END
END
CLOSE ListCurs
RETURN (@SwNList)
go

然后执行...

SELECT ShopName, dbo.SweetsList(ShopName) AS Sweets FROM Shops

Works in Sybase ASE...

CREATE FUNCTION SweetsList(@SN varchar(10))
returns varchar(255)
AS
DECLARE @SwNList varchar(255)
DECLARE @FetchSwN varchar(55)
DECLARE @Status INT, @Error INT

DECLARE ListCurs CURSOR FOR
SELECT SweetName
  FROM Sweets AS SW
 JOIN Shops AS SH
  ON SH.ShopID = SW.ShopID
 WHERE SH.ShopName = @SN
FOR READ ONLY

OPEN ListCurs
SELECT @Status = 0
WHILE @Status = 0
BEGIN
    FETCH ListCurs INTO @FetchSwN

    SELECT @Status = @@SQLSTATUS

    IF @Status = 0 
    BEGIN
       SELECT  @SwNList = CASE WHEN @SwNList IS NULL THEN '' ELSE @SwNList + ', ' END + @FetchSwN
    END
END
CLOSE ListCurs
RETURN (@SwNList)
go

Then execute ...

SELECT ShopName, dbo.SweetsList(ShopName) AS Sweets FROM Shops
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文