【问题标题】:Skip if value exist on SQL table如果 SQL 表上存在值则跳过
【发布时间】:2012-04-04 07:00:06
【问题描述】:

我有一个关于 SQL 的问题,我提供了一个小脚本,该脚本从 twitter 获取人们对某人的评价,并将其从我的服务器插入到 sql 表中,代码运行良好,但我的问题是,脚本如何跳过已经存在于表中的条目,因此它不会多次插入相同的条目,代码如下:

    <?php
include '../config/config.php';
mysql_connect($dbhost, $dbuser, $dbpwrd) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());

$rss = simplexml_load_file("http://search.twitter.com/search.rss?q=YaninGarciaD");
if($rss){
echo '<ul>';
foreach($rss->channel->item as $item){
            $author = (string) $item->author; // Title
            $link = (string) $item->link; // Url Link
            $description = (string) $item->description;
            $pubDate = (string) $item->pubDate; //Description

            list($usuario, $nombre3) = explode("@", $author);           
            list($basura, $nombre2) = explode(" (", $nombre3);
            $nombre = substr($nombre2, 0, -1);

            $piezasurl = explode("/", $link);
            $status = $piezasurl[5];
            $piezasfecha = explode(" ", $pubDate);

            if ($piezasfecha[2] == "Jan") {
            $mes = "01";
            } elseif ($piezasfecha[2] == "Feb") {
            $mes = "02";
            } elseif ($piezasfecha[2] == "Mar") {
            $mes = "03";
            } elseif ($piezasfecha[2] == "Apr") {
            $mes = "04";
            } elseif ($piezasfecha[2] == "May") {
            $mes = "05";
            } elseif ($piezasfecha[2] == "Jun") {
            $mes = "06";
            } elseif ($piezasfecha[2] == "Jul") {
            $mes = "07";
            } elseif ($piezasfecha[2] == "Ago") {
            $mes = "08";
            } elseif ($piezasfecha[2] == "Sep") {
            $mes = "09";
            } elseif ($piezasfecha[2] == "Oct") {
            $mes = "10";
            } elseif ($piezasfecha[2] == "Nov") {
            $mes = "11";
            } elseif ($piezasfecha[2] == "Dic") {
            $mes = "12";
            }

            $fecha = $piezasfecha[3].'-'.$mes.'-'.$piezasfecha[1].' '.$piezasfecha[4];


mysql_query("INSERT INTO yg_mensajes 
(nombre, mensaje, twitter, url, postid, tipo, fecha, aprovado)
VALUES ('$nombre', '$description', '$usuario', '$link', '$status', 2, '$fecha', 0 ) ") 
or die(mysql_error());              

echo '<li>';
echo $description.'<br />';
echo $usuario.'<br />';
echo $nombre.'<br />';
echo $link.'<br />';
echo $status.'<br />';
echo $fecha.'<br />';
echo '<br /></li>';

}
echo '</ul>';
}
?>

关于 twitter 的一个好处是帖子编号(在此代码中,是 var $status)是唯一的,即不应该在表中之前的 var

您可以在此处查看正在运行的脚本: http://www.yaningarcia.com/unete/test.php

我的SQL表结构是这样的:

 CREATE TABLE `yg_mensajes` (
  `id` int(11) NOT NULL auto_increment,
  `nombre` text,
  `mensaje` text,
  `twitter` text,
  `url` text,
  `postid` text,
  `tipo` int(11) default NULL,
  `fecha` datetime default NULL,
  `aprovado` int(11) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

谢谢!! :D

【问题讨论】:

  • 关于您的代码的一些提示。使用 pdo 而不是 mysql_* 函数。第二个是,如果你看到 elseif 太多,使用 switch-case。

标签: php sql insert skip


【解决方案1】:

您可以使用INSERT IGNORE...INSERT...ON DUPLICATE UPDATE.. 命令。

http://www.electrictoolbox.com/mysql-insert-ignore/

http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/

希望对你有帮助...

【讨论】:

  • 请注意,两者都是 MySQL 特定的。
【解决方案2】:

我认为,您需要在 postid 字段上创建索引并使用 INSERT IGNORE 命令。 或使用 INSERT...ON DUPLICATE KY UPDATE,但在这种情况下,您需要更新一个或多个字段 http://dev.mysql.com/doc/refman/5.6/en/insert.html

【讨论】:

    猜你喜欢
    • 2022-07-16
    • 1970-01-01
    • 2014-11-09
    • 2015-12-02
    • 2022-11-15
    • 2018-12-29
    • 1970-01-01
    • 2020-05-23
    相关资源
    最近更新 更多