MySQL 中的作用域派生表
假设我有 2 张桌子
一个名为篮子,
另一个名字叫水果。
篮子-
购物篮 ID 、购物篮名称
1 - 篮一
2 - 篮子里有两个
水果-
水果 ID 、购物篮 ID 、水果名称
1 - 1 - 香蕉
2 - 1 - 苹果
3 - 2 - 梨
从篮子中选择 *
JOIN (SELECT GROUP_CONCAT(fruit_id SEPARATOR ', ') FROMfruits WHEREbaskets.basket_id=fruits.basket_id) AS der_fruits
ON basset.basket_id=der_fruits.basket_id
现在,通过这个查询,我想获取 2 行(因为有 2 个篮子),其中包含水果 id 的列表。
像这样:
篮子id,水果
1 - 1, 2
2 - 3
但现在我得到的是这样的:
篮子id,水果
2 - 1, 2, 3
问题是,我必须在 DERIVED 表中传递全局baskets.basket_id 值。 MySQL 中有类似全局作用域的东西吗?
或者有没有办法在派生表内的变量中传递全局baskets.basket_id值?
Let's say I have 2 Tables
One named Baskets,
Another named Fruits.
Baskets-
basket_id , basket_name
1 - Basket One
2 - Basket Two
Fruits-
fruit_id , basket_id , fruit_name
1 - 1 - Banana
2 - 1 - Apple
3 - 2 - Pear
SELECT * FROM baskets
JOIN (SELECT GROUP_CONCAT(fruit_id SEPARATOR ', ') FROM fruits WHERE baskets.basket_id=fruits.basket_id) AS der_fruits
ON baskets.basket_id=der_fruits.basket_id
Now with this query I want to get 2 rows (since there are 2 baskets) with a list of the fruit id's in it.
Like this:
basket_id, fruits
1 - 1, 2
2 - 3
But just now what I get is this:
basket_id, fruits
2 - 1, 2, 3
The thing is, I have to pass the global baskets.basket_id value in the DERIVED table.
Is there anything like a global scope in MySQL?
Or is there a way to pass the global baskets.basket_id value in a variable inside that derived table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
水果是一个子查询。我不明白你为什么两次定义这种关系。你有什么想做的事我不明白吗?
The fruits are a subquery. I don't understand why you define the relationship twice. Is there something you are trying to do I don't understand?