插入PostgreSQL [英] INSERT INTO PostgreSQL

查看:114
本文介绍了插入PostgreSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用SQL时遇到了一些问题。
我想在表中插入2个值。

I have got a little problem with SQL. I'm trying to insert 2 values into my table.

这就是我的查询:INSERT INTO tableinfo(table,date)VALUES('Sell',' 8月24日);
但这不起作用。我有类似的东西:

that's my query: INSERT INTO tableinfo (table,date) VALUES ('Sell','24 August'); But it doesnt work. I've got something like that:

SQL error:
ERROR:  syntax near "INTO"
LINE 1: SELECT COUNT(*) AS total FROM (INSERT INTO tableinfo (table,...
                                              ^
In statement::
SELECT COUNT(*) AS total FROM (INSERT INTO tableinfo (table,date) VALUES ('Sell','24 August')) AS sub

基本的,所以我不知道为什么它不起作用:(
PostgreSQL 9.2.4

It's pretty basic so I don't know why it doesnt work :( PostgreSQL 9.2.4

推荐答案

它不是INSERT这就是问题所在,它是您要发出的无效SQL,请先尝试插入,然后再执行单独的count(*)查询,或者如果您使用的是PostgreSQL 9.1+,则可以使用公用表表达式和RETURNING

It's not the INSERT that is the problem, it is the invalid SQL that you are trying to issue. Try your insert first then a separate count(*) query, or if you are using PostgreSQL 9.1+ you can use Common Table Expressions and RETURNING

WITH ins AS (
     insert into tableinfo ("table","date") 
     values ('Sell','24 August') RETURNING "table"
)
select count(*) 
from ins;

这篇关于插入PostgreSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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