SQL 错误:整数的无效输入语法:“”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23911844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: invalid input syntax for integer: ""
提问by Trisna
I have this table tbl_buku:
我有这张桌子tbl_buku:
id_buku judul_buku tahun_buku
1 Bioogi 2010
2 Fisika 2010
3 Informatika 2012
4 Kimia 2012
I use query like this, but I am getting an error:
我使用这样的查询,但出现错误:
select case when t1.tahun_buku=t2.tahun_buku then ''
else t1.tahun_buku end tahun_buku,t1.judul_buku
from tbl_buku t1 left join tbl_buku t2
on t1.id_buku-1=t2.id_buku;
I want to show table like this:
我想像这样显示表格:
tahun_buku judul_buku
2010 Biologi
Fisika
2012 Informatika
Kimia
How to achieve this?
如何实现这一目标?
采纳答案by apomene
I think the problem in your query is that tahun_bukuis of datatype intand you are trying to select an empty string('').
我想在您的查询的问题是, tahun_buku是的datatype int,你是想选择一个空的string('')。
You have to workarounds:
你必须解决方法:
Change tahun_bukuto be varchar(2010,2012..will be consider as strings I dont know if it is ok)
更改tahun_buku为varchar(2010,2012..will 被视为字符串,我不知道是否可以)
Set:
放:
select case when t1.tahun_buku=t2.tahun_buku then null else t1.tahun_buku end tahun_buku,t1.judul_buku
from tbl_buku t1 left join tbl_buku t2
on t1.id_buku-1=t2.id_buku;
回答by Erwin Brandstetter
Use the window function
lag()to get values from the previous row.Use
NULLfor "empty" values in a numeric column- like @apomene already suggested.
The empty string''can only be used in character typesliketextorvarchar.Use
NULLIF()in this particular case to substitute the NULL value.
使用窗口函数
lag()从前一行获取值。使用
NULL了一个“空”值数值列-像@apomene已经建议。
空字符串''只能用于像或这样的字符类型。textvarchar使用
NULLIF()在这种特殊情况下替换空值。
SELECT NULLIF(lag(tahun_buku) OVER (ORDER BY tahun_buku, judul_buku)
, tahun_buku) AS tahun_buku
, judul_buku
FROM tbl_buku
ORDER BY tahun_buku, judul_buku;

