如何在Sqlite中的列中显示行数据 [英] How Do I Display Row Data In Column In Sqlite

查看:68
本文介绍了如何在Sqlite中的列中显示行数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三张桌子

第一张桌子包含语言(id,languagename)英文,中文等,

第二张桌子包含其翻译。

现在我想将我的第一个表格行转换为列并并排显示数据。



任何帮助。

解决方案

由于SQLLite没有PIVOT功能,你可以做一个技巧来完成它。

创建一个结构与你想要输出相同的临时表是。然后使用选择查询从转换表将数据插入临时表。然后显示临时表的结果,最后删除临时表。



例如:

语言表

 Id Lang 
1英语
2中文
3印地语





翻译表

 LangId Trans 
1这是一个示例文本
1这是另一个示例文本
2让我们假设它是中文
3假设它是在印地文中





临时表

 创建  TABLE  temp_table(EngTrans  VARCHAR  255 ),ChiTrans  VARCHAR  255 ),HinTrans  VARCHAR  255 ))

INSERT INTO temp_table (EngTr ans)
SELECT Trans FROM TransTable
WHERE Id = 1

INSERT INTO temp_table( ChiTrans)
SELECT Trans FROM TransTable
WHERE Id = 2

INSERT INTO temp_table( HinTrans)
SELECT Trans FROM TransTable
WHERE Id = 3





您可以循环使用Language表并使insert语句动态化,但我是不确定SQLLite是否支持循环或游标。



虽然它不是直接的答案,但可以完成这项工作。

希望,它有帮助:)


hi,i have three tables
first table contains languages(id,languagename) english, chinese, etc.,
second table contains its translations .
now i want to convert my first table rows into columns and display the data side by side.

any help.

解决方案

As SQLLite doesn't have PIVOT feature, you can do a trick to get it done.
Create a temporary table with the structure same as you want your output be. Then insert the data using select query from the translation table to the temporary table. Then show the result from temporary table and at last drop the temporary table.

For Example:
Language Table

Id	Lang
1	English
2	Chinese
3	Hindi



Translation Table

LangId	Trans
1	this is a sample text
1	this is another sample text
2	let suppose it is in chinese
3	let suppose it is in hindi



Temp Table

CREATE TABLE temp_table(EngTrans VARCHAR(255), ChiTrans VARCHAR(255), HinTrans VARCHAR(255))

INSERT INTO temp_table (EngTrans)
SELECT Trans FROM TransTable
WHERE Id=1

INSERT INTO temp_table (ChiTrans)
SELECT Trans FROM TransTable
WHERE Id=2

INSERT INTO temp_table (HinTrans)
SELECT Trans FROM TransTable
WHERE Id=3



You can loop for the Language table and make the insert statement dynamic, but I am not sure if SQLLite supports loop or cursor.

Although it is not a direct answer but can do the job.
Hope, it helps :)


这篇关于如何在Sqlite中的列中显示行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆