网站开发中常用的SQL批处理语句
以下是收集的在网站开发过程中常用SQL数据库批量处理语句。
1、对数据库的数据表的值进行运算,然后将运算结果替换原来的值:
update `wp_postmeta` set `meta_value`=`meta_value`+60 where `post_id`='24628' and `meta_key`='price';
2、给数据库里某个字段下的所有值,后面追加一个字符;使用CONCAT()函数;
update `wp_postmeta` set `meta_value`=CONCAT(`meta_value`,'个') where `post_id`='5940' and `meta_key`='次数';
3、一次性将数据库字段里所有值替换成一个新值:
update `wp_postmeta` set `meta_value`='新值' where `post_id`='5917' and `meta_key`='造价';
4、PHP语句批量修改替换旧的内容为新内容
for($i=0;$i<count($postid2);$i++){
$sql = "UPDATE wp_posts SET post_content = REPLACE(post_content, '旧内容', '新内容') where ID = 33";
mysqli_query($conn,$sql);//执行SQL
}
$r=mysqli_query($conn,$sql);
5、PHP查询数据库中不等于某个值的500条数据
mysqli_query($conn,"select * from pre_common_member where sousuleixing <> '' ORDER BY nowtimes DESC limit 0,500");//获取最新500条数
6、使用SQL插入一个新字段和新数据
insert into wp_postmeta(post_id,meta_key,meta_value) values ('1772','taobaofb','是');
7、如果不知道一个字段存不存在,如果存在就修改,如果不存在就插入
$sql = "REPLACE INTO wp_postmeta SET post_id = $postid,meta_key = 'baijiahao',meta_value = $newleirong";
8、满足多条件的查询
SELECT * FROM `wp_banquan` WHERE email LIKE '%@%' AND mobanurl LIKE '%http%' AND mobanurl NOT LIKE '%undefined%' order by ID desc limit 0,50
9、同时查询多个数据库满足多个条件
$vars = $wpdb -> get_results("SELECT * FROM `wp_posts` where post_status='publish' and post_type='post' and (post_title like '%".$catsarr01[$i]."%') and ID IN (SELECT object_id FROM `wp_term_relationships` WHERE term_taxonomy_id IN ".$showcatarr.") ORDER BY ID DESC limit ".$fromshu01[$i].",7", ARRAY_A);
10、使用update from将查询出来的结果ID和希望实现的更新结果相关联:
UPDATE user_table SET Address="Shanghai" FROM
(SELECT id FROM user_table WHERE address="Beijing") as b
WHERE b.id = user_table.id;
11、如果要从某个条件取值后,再查询
$result = mysqli_query($conn,"SELECT post_id FROM `wp_postmeta` WHERE meta_key='title_link' and meta_value='$wzmeta0[$i]'");//查询得到ID
while($row = mysqli_fetch_array($result))//转成数组,且返回第一条数据,当不是一个对象时候退出
{
$pid = $row['post_id'];
$sql1 = "UPDATE `wp_postmeta` SET `meta_value`='$wzmeta1[$i]' WHERE post_id = '$pid' and meta_key='bigfa_download'";
mysqli_query($conn,$sql1);
}