Android Sqlite 将行拖为列、数据透视?
开始知道它被称为枢轴,但无法在没有聚合的情况下获得 sqlite 的示例。
架构:
Readonly Table foos(_id, foo) (10 max records)
Readonly Table bars(_id, bar) (300 max records)
Readonly Table items(_id, foo_id, bar_id, item1, item2) (3000 max records)
所需输出:
Select
foos._id,
foos.foo,
bars.bar,
items.item1(bar_id=1),
items.item1(bar_id=2),
items.item2(bar_id=1)
我总是需要两列。
我错过了一些非常明显的东西吗? 我的性能会受到影响吗? 我的桌子设计错了,纠正我!
更新:
SELECT
foo_id,
GROUP_CONCAT(CASE bar_id WHEN 1 THEN title ELSE NULL END) AS 'Item1',
GROUP_CONCAT(CASE bar_id WHEN 2 THEN title ELSE NULL END) AS 'Item2'
FROM
items
WHERE
foo_id=2
GROUP BY
foo_id
这就是最终的结果。
Came to know its called pivots, but couldn't get examples for sqlite without aggregation.
Schema:
Readonly Table foos(_id, foo) (10 max records)
Readonly Table bars(_id, bar) (300 max records)
Readonly Table items(_id, foo_id, bar_id, item1, item2) (3000 max records)
Output Required:
Select
foos._id,
foos.foo,
bars.bar,
items.item1(bar_id=1),
items.item1(bar_id=2),
items.item2(bar_id=1)
i always need two columns.
Am i missing something very obvious.
do i get a performance hit.
is my table design wrong, correct me!
UPDATE:
SELECT
foo_id,
GROUP_CONCAT(CASE bar_id WHEN 1 THEN title ELSE NULL END) AS 'Item1',
GROUP_CONCAT(CASE bar_id WHEN 2 THEN title ELSE NULL END) AS 'Item2'
FROM
items
WHERE
foo_id=2
GROUP BY
foo_id
this is what is ended up with.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该查看 Common MySql Queries,有很多示例,包括数据透视表,这可能对您有帮助。
您可以在此部分中找到您要查找的内容。
You should check out the Common MySql Queries, there are a lot of samples, including the pivots, which may help you.
You can find what you are looking for in this section.