“连接时连接”的 SQL 查询
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您必须指定您正在使用的 DBMS。
MySQL 的 GROUP CONCAT 是正是您所需要的。
You'll have to specify what DBMS you're using.
MySQL's GROUP CONCAT is exactly what you need.
这是一种交叉表查询,不可能在一个查询中使用 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.
我在 SQL Server 上对此进行了测试,但希望它也适用于 Sybase。如果没有,也许它会让你足够接近解决它。
如果我创建这个函数:
然后我可以执行这个查询并获得您想要的结果:
希望这会有所帮助。
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:
I can then execute this query and get the results you want:
Hope this helps.
不幸的是,adrift的答案中的方法不适用于Sybase ASE的select语句,变量@list不会更新每行,它仅适用于最后一行。但是因为更新对每一行执行并且表的大小不会很大,所以您可以使用更新语句来完成。小例子:
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:
P.S. As for me, cursor isn't good way...
在 Sybase ASE 中工作...
然后执行...
Works in Sybase ASE...
Then execute ...