bit(1)在"MySQL"中的类型列中插入值的正确语法是什么?
我的列定义是:
payed bit(1) NOT NULL
Run Code Online (Sandbox Code Playgroud)
我正在从csv数据保存的位置加载数据0或1.我尝试使用以下方法进行插入:
b'value' or 0bvalue (example b'1' or 0b1)
Run Code Online (Sandbox Code Playgroud)
如手册所示.
但我一直收到这个错误:
Warning | 1264 | Out of range value for column 'payed' at row 1
Run Code Online (Sandbox Code Playgroud)
插入bit值的正确方法是什么?
我没有手动插入,但我正在从csv(使用load data infile)加载数据,其中列的数据是0或1.
这是我的load查询,我已经重命名了隐私问题的字段,该定义中没有错误:
load data local infile 'input_data.csv' into table table
fields terminated by ',' lines terminated by '\n'
(id, year, field1, @date2, @date1, field2, field3, field4, …Run Code Online (Sandbox Code Playgroud) 我一直在阅读如何在重复键上使用 MySQL 插入,看看它是否能让我避免选择一行,检查它是否存在,然后插入或更新。然而,当我阅读文档时,有一个方面让我感到困惑。这是文档所说的:
如果您指定 ON DUPLICATE KEY UPDATE,并且插入的行会导致 UNIQUE 索引或 PRIMARY KEY 中的重复值,则执行旧行的 UPDATE
问题是,我不想知道这是否适用于我的问题,因为我不插入新的“条件”是存在两列等于某个值的行,不一定主键是一样的。现在我想象的语法是这样的,但我不知道它是否总是插入而不是替换:
INSERT INTO attendance (event_id, user_id, status) VALUES(some_event_number, some_user_id, some_status) ON DUPLICATE KEY UPDATE status=1
Run Code Online (Sandbox Code Playgroud)
问题是, event_id 和 user_id 不是主键,但是如果“出席”表中的一行已经包含具有这些值的那些列,我只想更新它。否则我想插入它。这甚至可以通过 ON DUPLICATE 实现吗?如果没有,我可以使用什么其他方法?
我偶然发现了一个非常奇怪的行为:
想象一下,我们有一个表客户:
MariaDB [connections]> describe customers;
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| customerId | int(11) | NO | PRI | NULL | auto_increment |
| customerName | varchar(50) | NO | | NULL | |
+--------------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)
插入几个值:
insert into customers(customerName) values('Foo');
insert into customers(customerName) values('Bar');
Run Code Online (Sandbox Code Playgroud)
然后删除所有内容并重置自动增量:
DELETE FROM customers;
ALTER TABLE customers AUTO_INCREMENT = 0;
Run Code Online (Sandbox Code Playgroud)
现在,使用customerId = 0插入一个新值:
INSERT INTO customers(customerId,customerName) VALUES(0,'Site owner');
Run Code Online (Sandbox Code Playgroud)
并看到结果: …
I表1包含:
|col1|
| 1 |
| 2 |
| 1 |
| 3 |
| 1 |
| 2 |
| 4 |
| 2 |
| 3 |
| 1 |
Run Code Online (Sandbox Code Playgroud)
我有另一个表有一个列名val,我的代码是
INSERT INTO table2(value) VALUES ((select distinct col1 from table1))
Run Code Online (Sandbox Code Playgroud)
我得到了 #1242 - 子查询返回超过 1 行。
如何将多行插入到我的 table2 中?
这看起来非常简单,但我坚持使用一个简单的插入语句。见下文:
begin work;
CREATE TEMPORARY TABLE IF NOT EXISTS insert_table AS
(
select
r.resource_id
,fr.file_repos_id
,mv.VALUE
from
resources r
join versions v on v.RESOURCE_ID = r.resource_id
join metadata_values mv on mv.resource_id = r.resource_id
join file_repository fr on fr.file_repos_id = v.foreign_id
where
v.version_status = 'C'
and r.RESOURCE_TYPE = 4
and fr.file_title in ('suburbs')
);
insert
into
metadata_values (elem_id,value,resource_type,resource_id,foreign_id,mtvr_id,mett_id)
values
(62,'test',4,insert_table.resource_id,insert_table.file_repos_id,80,4);
rollback work;
Run Code Online (Sandbox Code Playgroud)
在临时表行中fr.file_title in ('suburbs'),实际列表是从其他地方动态拉取的(这是出于演示目的)。我收到以下错误消息:
错误代码:1054。“字段列表”中的未知列“insert_table.resource_id”
现在,我可以在临时表上运行一个选择,它返回正常,只是在更新语句中失败。我正在从 MySQL 工作台运行它。完全不知道这里发生了什么。
我正在尝试做一个本质上非常简单的任务,结果是:
ORA-00928:缺少SELECT关键字
我要做的就是将结果持久化periods到table中globalTable。选择工作正常(我返回了行),但是一旦将其替换为“插入”,就会出现上述错误。
create global temporary table globalTable
(
ids number(11)
);
with periods as
(
select cl.id uniqueId
from inv_mpan_hh_con_lines cl
left join invoice_vat_lines vl on
cl.invoice_id = VL.INVOICE_ID
where rownum < 4
)
--//Issue occurs at insert keyword. If I comment it and uncomment select it works as expected//--
--select uniqueId
insert into globalTable
from periods;
Run Code Online (Sandbox Code Playgroud)
非常感谢任何指针。
我正在尝试将分组记录的计数导出到 Excel。
我有一个看起来像这样的 CSV 文件:
Date Country Sub Source
2014-09-11 US 1 source1
2014-09-11 US 2 source2
2014-09-11 UK 1 source3
2014-09-11 US 1 source1
2014-09-11 IN 3 source4
Run Code Online (Sandbox Code Playgroud)
我需要按 Country、Sub 和 Source 分组的记录数。
df = pd.read_csv('log.csv',sep='\t')
count = df.groupby(['Country','Sub','Source']).size()
count.to_excel('report.xls', index=False)
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时出现错误: AttributeError: 'Series' object has no attribute 'to_excel'
是否不允许将 DataFrameGroupBy 导出到 Excel?还有一种方法可以将带有计数信息的分组记录插入到 MySQL 数据库中吗?
我正在使用 oracle sql developer 在我的数据库中插入行。
当此请求有效时:
INSERT INTO TABLE ( USERID, USERNAME) VALUES (1,"ok1")
Run Code Online (Sandbox Code Playgroud)
第二个(当我尝试插入多行时)不起作用:
INSERT INTO TABLE ( USERID, USERNAME) VALUES (1,"ok1"),(2,"ok2")
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Erreur SQL : ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
Run Code Online (Sandbox Code Playgroud) 我试着保存到Sqlite Event
public class EventDao{
private SQLiteStatement insertStatement;
private SQLiteDatabase db;
private static final String INSERT =
"insert into " + EventsTable.TABLE_NAME + "(" + EventsColumns.WHO + ", "
+ EventsColumns.WHAT
+ ") values (?, ?)";
public EventDao(SQLiteDatabase db) {
this.db = db;
Log.w(TAG, EventDao.INSERT);
insertStatement = db.compileStatement(EventDao.INSERT);
}
public long save(Event type) {
insertStatement.clearBindings();
insertStatement.bindString(1, type.getmWho());
insertStatement.bindString(2, type.getmWhat());
return insertStatement.executeInsert();
}
//...
}
Run Code Online (Sandbox Code Playgroud)
一切工作确定,当两者getmWho()并getmWhat()返回非null数值.但是当我尝试保存具有Who字段为空的事件(并因此getmWho()返回null)时,我收到错误
java.lang.IllegalArgumentException: the bind …Run Code Online (Sandbox Code Playgroud) 我试图将从我的 select 语句中得到的所有错误插入到一个错误表中,但我没有让它起作用。
BEGIN TRY
INSERT INTO [database]..[Table]([Column1], [Column2], [Column3])
SELECT
[Column1], [Column2], [Column3]
FROM
[database]..[SourceTable]
END TRY
BEGIN CATCH
INSERT INTO [database]..[ErrorTable]([Column1], [Column2], [Column3])
-- What to do here? .... (select the same query as up?)
SELECT
[Column1], [Column2], [Column3]
FROM [database]..[SourceTable]
END CATCH
Run Code Online (Sandbox Code Playgroud)
现在我想查看我的Error表格中的所有错误以及所有在表格中正确显示的错误,但我没有让它起作用?
源系统的数据类型是nvarchar,我的列是INT因为值应该只是 INT ..所以源系统的设计很糟糕?