Sqlite默认保存数据是用UTF8格式,而现有程序开发工具都是默认GB2312的格式,所以你编程写的中文不转码直接写到库里后,用任何数据库工具看肯定都是乱码。但是读出来之后仍然是GB2312,所以显示正常。反之如果你用数据库工具写中文,则默认存UTF8格式,用程序读出来就是乱码。也就是说的不可兼得。 如果要实现能够编程和数据库工具读写都正常,必须要转码。也就是说,保存时候,要把GB2312转成UTF8,而读出时候需要把UTF8在转成GB2312。 网上有源程序可用,叫做CCodingConv的类,你搜一个源码,在每次执行SQL语句之前,将其转换一下即可。将整个SQL语句进行转换,E文的会保持不变,而中文的会自动转换成合适的编码,执行即可实现要求。
create table tb(id int,st datetime,et datetime,qty int)
insert into tb select 1111,'2009-01-01','2009-01-04',200
insert into tb select 1111,'2009-01-05','2009-01-08',300
with cte as
(
select id,st,qty,et from tb
union all
select aid,dateadd(dd,1,ast),aqty,aet from cte a join tb b on aet=bet and ast<bet
)
select id,st,qty from cte order by st
id st qty
----------- ----------------------- -----------
1111 2009-01-01 00:00:00000 200
1111 2009-01-02 00:00:00000 200
1111 2009-01-03 00:00:00000 200
1111 2009-01-04 00:00:00000 200
1111 2009-01-05 00:00:00000 300
1111 2009-01-06 00:00:00000 300
1111 2009-01-07 00:00:00000 300
1111 2009-01-08 00:00:00000 300
(8 行受影响)
with cte(l1,l2,new_l2)as
(
select l1, l2, l2 + '(' + cast(ROW_NUMBER() over(partition by l1 order by l1,l2) as varchar(10)) + ')' as new_l2
from table1
)
update cte set l2 = new_l2
谢谢 一楼,又学到一些东西。
WITH CTE
AS(
SELECT tc1supplierid AS supplierid,
tssuppliercode AS suppliercode,
tssuppliername AS suppliername,
tscontactperson AS contractperson,
tsphone AS phone,
tsregistercapital AS registercapital,
tsprimarybusiness AS primarybusiness,
tscategorycode AS categorycode,
tsrepresentative AS representative,
tsaddress AS address
FROM t_supplier ts
JOIN t_contract tc1
ON tssupplierid=tc1supplierid AND tc1state='co0105'
JOIN t_orgnization tor1
ON tor1orgnizationid = tc1orgnizationid
GROUP BY tc1supplierid,
tssuppliercode,
tssuppliername,
tscontactperson,
tsphone,
tsregistercapital,
tsaddress,
tsprimarybusiness,
tscategorycode,
tsrepresentative
),
CTE2
AS(
SELECT supplierid AS supplierid,
state AS state,
COUNT(contractid) AS contracttime
FROM t_contract
GROUP BY supplierid,state
)
SELECT C1supplierid,
C1suppliercode AS suppliercode,
C1suppliername AS suppliername,
C1contractperson AS contractperson,
C1phone AS phone,
C1registercapital AS registercapital,
C1primarybusiness AS primarybusiness,
C1categorycode AS categorycode,
C1representative AS representative,
C1address AS address,
C2contracttime AS contracttime
FROM CTE C1
JOIN CTE2 C2
ON C1supplierid = C2supplierid AND C2state='co0105'
ORDER BY contracttime DESC
提供思路:把所有数据放进一个大MAP,map1中,把更新完的数据放到小map中map2,以ID为KEY,整条数据为VALUE;下次再更新的话,将map1中key值等于map2的key值的数据,去掉;再更新
如果你每页显示10条记录那么你就每次查询10条记录 在Oracle数据库中:方法一:
SELECT
FROM databasetest t1
WHERE (SELECT count() FROM databasetest t2 WHERE t2id < t1id) >= 11 AND
(SELECT count() FROM databasetest t2 WHERE t2id < t1id) < 20
花费时间: 62156秒方法二:
select
from (select rownum as num,id,test1,test2 from databasetest a where rownum < 20)
where num > 11;
花费时间: 0094秒
解释:rownum意为读取行号,首先读取小于20行的记录,然后在这些记录中读取行号大于10的记录,行号是整个表的同一分配。
方法三:
(select from databasetest where rownum<=20) minus (select from databasetest where rownum<11)
花费时间: 011秒
在SQL Server中方法一:
SELECT
FROM databasetest t1
WHERE (SELECT count() FROM databasetest t2 WHERE t2id < t1id) >= 11 AND
(SELECT count() FROM databasetest t2 WHERE t2id < t1id) < 20
花费时间:id未加索引为18秒,加上索引后为7秒
方法二:
select top 10 from databasetest where id not in (select top 10 id from databasetest);
花费时间: 小于1秒
解释:首先查找整个表的前10个记录,然后除前10个记录的其它记录中找前面10个记录。可理解为:在表中,除了前面10个记录,找出靠前的10个记录总结:
在Oracle最佳查询为第二种方法,使用rownum函数,在SQL Server中最佳查询为第二种方法,使用Top函数。
其中第一种方法使用于任何数据库。为了减少网络通信,同时又提高查询速度,可以使用缓冲。即一次查询足够多的记录,保存在缓存中,传给客户,当客户需要查看指定记录时,从缓存中取出数据。具体实现方案为:假如每页10条记录,如果查看第5页记录,则一次查找的40-69共三十条记录,存入缓存。当选择上一页,下一页时从缓存中读出数据,当查找第7页的数据时,再查找6-8页数据。减少了与数据库的网络通信,同时又提高了效率。
SQL语句实现数据分页(SQLServer)
SQLServer的分页依靠的是top这个属性。1分页方案一:(利用Not In和SELECT TOP分页)SELECT TOP 页大小
FROM TestTable
WHERE (ID NOT IN
(SELECT TOP 页大小页数 id
FROM 表
ORDER BY id))
ORDER BY ID2分页方案二:(利用ID大于多少和SELECT TOP分页)SELECT TOP 页大小
FROM TestTable
WHERE (ID >
(SELECT MAX(id)
FROM (SELECT TOP 页大小页数 id
FROM 表
ORDER BY id) AS T))
ORDER BY ID常用的是方案1这个不需要多说!方案2有局限性。
列完全重复时,SQL05以上版本用CTE。SQL2000用临时表和辅助列
如:
T2:
ID Name
1 Name
1 Name
2 Name2
alter table t add row int identity
select from t2 a where row=(select min(row) from t2 where ID=aID and Name=aName)
update
以上就是关于sqlite 支持cte写法吗全部的内容,包括:sqlite 支持cte写法吗、数据库sql语句问题、问个高级点的数据库插入数据SQL语句问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)