标签: insert-into

仅当记录尚不存在时才INSERT INTO查询

在我的表中,我有两个字段:v_idip_address.我想仅在IP地址尚不存在时才将一些数据插入此表.

谷歌之后我遇到了INSERT IGNORE INTO声明,这是我的代码:

public function update_visits($ip_address)
{
    $sql = 'INSERT IGNORE INTO `24h_visits` (ip_address) VALUES (?)';
    if($this->db->query($sql, array($ip_address)))
    {
        return TRUE;
    }
    return FALSE;
}
Run Code Online (Sandbox Code Playgroud)

它运行正常且没有错误,但是仍然会生成重复的行,即使将相同的IP作为参数传入也是如此.

有人知道吗?谢谢.

mysql sql insert-into

3
推荐指数
2
解决办法
1527
查看次数

从一个表中选择字段并将INSERT转换为另一个表的方法?

我有两个表,TableA和TableB

TableA有9个字段TableB有7个字段

两个表中有两个相同的字段(id和name),有没有办法从TableA中选择这两个字段并将它们插入TableB?

我使用以下语句查看了INSERT INTO ... SELECT方法:

INSERT INTO TableB
SELECT id, name
FROM TableA
WHERE id = 1
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

#1136 - Column count doesn't match value count at row 1
Run Code Online (Sandbox Code Playgroud)

我假设这个错误不允许我只在表中插入2个字段?如果是这样,有没有办法绕过这个或另一种方法?

谢谢

mysql insert-into

3
推荐指数
1
解决办法
1251
查看次数

如何在SQL中向新表添加行号?

我正在尝试使用已经使用的现有表创建一个新表:

INSERT INTO NewTable (...,...)
   SELECT * from SampleTable
Run Code Online (Sandbox Code Playgroud)

我需要的是在开头或结尾添加一个记录号,只要它在那里就没关系.

样本表

Elizabeth  RI 02914
Emily      MA 01834
Run Code Online (Sandbox Code Playgroud)

预期新表

1 Elizabeth  RI 02914
2 Emily      MA 01834
Run Code Online (Sandbox Code Playgroud)

那可能吗?

这就是我最终拍摄的内容......除了现在这些表格的大小不同,因为我需要我的ErrorTemporaryTable有一个列,其中第一行的数字比前一个增加一个.

declare @counter int
declare @ClientMessage varchar(255)
declare @TestingMessage carchar(255)
select @counter = (select count(*) + 1 as counter from ErrorValidationTesting)
while @counter <= (select count(*) from ErrorValidationTable ET, ErrorValidationMessage EM where ET.Error = EM.Error_ID)
begin
    insert into ErrorValidationTesting (Validation_Error_ID, Program_ID, Displayed_ID, Client_Message, Testing_Message, Create_Date)
    select * from ErrorTemporaryTable

    select @counter = @counter …
Run Code Online (Sandbox Code Playgroud)

sql-server-2005 insert-into

3
推荐指数
2
解决办法
3万
查看次数

PDO截断字符串参数

我有这个函数来向表中插入数据:

public static function insertSistema($name,$description,$created_at, $updated_at, $img_file_name, $img_content_type, $img_file_size, $img_updated_at, $visible, $description, $access_floors, $access_procedures, $access_datas, $access_histories, $access_incidences, $access_operations, $access_reports, $access_messagings)
{
    $conector = new Conexion("localhost","xrem_prueba");
    try
    {
        $con = $conector->Conectar();
        $con->exec('SET CHARACTER SET utf8');
        $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $consulta = $con->prepare("INSERT INTO systems (name, created_at, updated_at, img_file_name, img_content_type, img_file_size, img_updated_at, visible, description, access_floors, access_procedures, access_datas, access_histories, access_incidences, access_operations, access_reports, access_messagings ) VALUES (:name, :created_at, :updated_at, :img_file_name, :img_content_type, :img_file_size, :img_updated_at, :visible, :description, :access_floors, :access_procedures, :access_datas, :access_histories, :access_incidences, :access_operations, :access_reports, :access_messagings);");
        $consulta->bindParam(':name', $name, PDO::PARAM_STR, …
Run Code Online (Sandbox Code Playgroud)

php mysql pdo insert-into

3
推荐指数
1
解决办法
905
查看次数

可以插入表的最大列数/值 - mysql

有谁知道你可以插入表(mysql)的最大列数/值是多少?我得到一个错误... IDK ... 20列/值?(是的,#_ of_col = #no_of_val)

INSERT INTO comenzi (a,b,c,d,e,f,...) 
VALUES (1,2,3,4,5,6,...)
Run Code Online (Sandbox Code Playgroud)

mysql insert-into

2
推荐指数
1
解决办法
3729
查看次数

将唯一值插入数据库PGSQL/MSSQL

我正在尝试使用插入到不存在的地方在MSSQL和Postgresql数据库中插入唯一记录.但是我收到的语法错误不正确,如下所示.我究竟做错了什么?

INSERT INTO settings (id, title, description)
VALUES  (1, 'imageHeight', 'Image Height')
WHERE NOT EXISTS (Select * from settings where id = 1);
Run Code Online (Sandbox Code Playgroud)

错误: [Macromedia] [SQLServer JDBC驱动程序] [SQLServer]关键字"WHERE"附近的语法不正确.

sql sql-server postgresql not-exists insert-into

2
推荐指数
1
解决办法
526
查看次数

处理插入到选择中的异常

方案1

我有表交易表

create table TXN_HEADER
(
  txn_id         NUMBER(10) not null,
  txn_Date      date 
  product_id    NUMBER(10),
  company_id    NUMBER(10),
  dealer_id     NUMBER(10),
  tran_amt      number(10,2)
)
Run Code Online (Sandbox Code Playgroud)

上表具有对product.product_id 和company.company_id 的外键引用。

该表有 5m 行

模式2

create table TXN_HEADER_REPORTS
(
  txn_id         NUMBER(10) not null,
  txn_Date      date 
  product_id    NUMBER(10),
  company_id    NUMBER(10),
  dealer_id     NUMBER(10),
  tran_amt      number(10,2)
)
Run Code Online (Sandbox Code Playgroud)

这里我们也有相同的约束,具有对product.product_id和company.company_id的外键引用。

在模式 2 中,我们尝试一次性插入从模式 1 到模式 2 的所有行,如下所示

begin 
  insert into TXN_HEADER_REPORTS (
  txn_id, txn_Date ,product_id,company_id   ,  dealer_id ,  tran_amt)
  select 
  txn_id, txn_Date ,product_id,company_id   ,  dealer_id ,  tran_amt
  from schema1.TXN_HEADER;
  commit;
exception   
  when …
Run Code Online (Sandbox Code Playgroud)

oracle plsql insert-into

2
推荐指数
1
解决办法
4528
查看次数

插入w /多个选择给出ERROR 1242:子查询返回多于1行

我有表foo1的列UserID,TimeStamp; foo2,列为userID,Level&table为foo3,列为userID,Timestamp.

我想从表foo2中存在UserID的foo3中插入foo1的所有行.

我收到错误1242:子查询返回超过1行以下

INSERT into foo1 (UserID,TimeStamp)
SELECT  
(SELECT UserID from foo2 as UserID),

(SELECT foo3.TimeStamp
from foo3
inner join foo2
ON foo3.UserID=foo2.UserID) as TimeStamp
Run Code Online (Sandbox Code Playgroud)

insert-into mysql-error-1242

2
推荐指数
1
解决办法
849
查看次数

使用PHP $变量进行MySQL INSERT INTO

我可以在那里使用$ variable$strSQL = "SELECT * FROM $person"; 但是我不能在那里使用$ variable$sql = "INSERT INTO $person . . .

`$person` 
Run Code Online (Sandbox Code Playgroud)

不要工作......

有什么区别?我如何使用$ variable而不是表的名称(INSERT INTO table_name)

$sql = 'INSERT INTO $person (date, min, sec, count, temp, universal)
    VALUES("'.$date.'", "'.$min.'", "'.$sec.'", "'.$count.'", "'.$temp.'", "'.$universal.'")';
    if(!mysql_query($sql))
    {echo '<center><p><b>ERROR!</b></p></center>';}
    else
    {echo '<center><p><b>Its okay</b></p></center>';}
        }
Run Code Online (Sandbox Code Playgroud)

解决:

mysql_query("INSERT INTO $person (date, min, sec, count, temp, universal) VALUES('$date', '$min', '$sec', '$count', '$temp', '$universal')") or die(mysql_error());
Run Code Online (Sandbox Code Playgroud)

php mysql variables insert-into

2
推荐指数
1
解决办法
1万
查看次数

如何在oracle数据库表中插入n-1列值

我有2个表Tab1和Tab2.Both有相同的列,除了Tab2有一个额外的列而不是Tab1.Now我想从Tab2插入数据到Tab1.

  • TAB1:COL1,COL2 ..... col100
  • TAB2:COL1,COL2 ..... col100,col101

我怎样才能做到这一点.

我不想在插入过程中提到列名.

可以做到这一点.

谢谢

sql oracle insert-into

2
推荐指数
1
解决办法
278
查看次数