Joyber 发布的文章

MySQL中常用去重复数据的方法是使用 distinct 或者 group by ,以上2种均能实现,但2者也有不同的地方。

distinct 特点:
如:select distinct name, sex from tb_students 这个sql的语法中,查询 tb_students 表中 name, sex 并去除名字和性别都重复的学生:

1、distinct 只能放在查询字段的最前面,不能放在查询字段的中间或者后面。

备注:select sex, distinct name from tb_students 这种写法是错误的,distinct 只能写在所有查询字段的前面
单独的字段这样也行
select count(distinct sex) from tb_students

2、distinct 对后面所有的字段均起作用,即 去重是查询的所有字段完全重复的数据,而不是只对 distinct 后面连接的单个字段重复的数据。

备注:也就是 distinct 关键字对 name, sex 都起作用,去重姓名、性别完全一样的学生,如果姓名相同、性别不同是不会去重的。

3、要查询多个字段,但只针对一个字段去重,使用 distinct 去重的话是无法实现的。

group by 特点:
1、一般与聚类函数使用(如count()/sum()等),也可单独使用。

2、group by 也对后面所有的字段均起作用,即 去重是查询的所有字段完全重复的数据,而不是只对 group by 后面连接的单个字段重复的数据。

3、查询的字段与 group by 后面分组的字段没有限制。

这里只是一个阅读笔记


1)简单的赋值

->where("id=:id", [
    'id' => 1
])
上面的程式等同于 id=1


2)AND查询
->where("id=:id and pack_name=:pack_name", [
    ':id' => 1,
    ':pack_name' => 'com.famigo.sandbox'
])
或者

->where([
    'and',
    'id=:id',
    'pack_name=:pack_name'
], [
    ':id' => 1,
    ':pack_name' => 'com.famigo.sandbox'
])
这两种程式等同于 id=1 AND pack_name='com.famigo.sandbox'

3)OR查询

->where("id=:id or pack_name=:pack_name", [
    ':id' => 1,
    ':pack_name' => 'com.famigo.sandbox'
])
或者
->where([
    'or',
    'id=:id',
    'pack_name=:pack_name'
], [
    ':id' => 1,
    ':pack_name' => 'com.famigo.sandbox'
])
这两种程式等同于 id=1 OR pack_name='com.famigo.sandbox'
4)AND OR 混合查询

->where([
    'and',
    'display=:display',
    [
        'or',
        'id=:id1',
        'id=:id2'
    ]
], [
    ':display' => 1,
    ':id1' => 1,
    ':id2' => 2
])
上面的程式等同于(display=1) AND ((id=1) OR (id=2))
5)IN查询

->where([
    'in', 'id', [1, 3, 5, 6]
])
上面程式等同于 id in (1, 3, 5, 6)
->where([
    'and',
    'display=:display',
    'lang=:lang',
    [
    'in', 'id', [1, 3, 5, 6]
    ]
], [
    ':display' => 1,
    ':lang' => 2
])
上面程式等同于 (display=1) AND (lang=2) AND (`id` IN (1, 3, 5, 6))
更为麻烦点的例子

->where([
    'or',
    [
        'and',
        'display=:display1',
        [
        'in', 'id', [1, 3, 5, 6]
        ]
    ],
    [
        'and',
        'display=:display2',
        [
        'in', 'id', [2, 4, 8, 9]
        ]
    ]
], [
    ':display1' => 1,
    ':display2' => 2,
])
上面程式等同于((display=1) AND (`id` IN (1, 3, 5, 6))) OR ((display=2) AND (`id` IN (2, 4, 8, 9)))
6)NOT IN 查询

->where([
    'not in', 'id', [1, 2, 4, 3]
])
上面程式等同于`id` NOT IN (1, 2, 4, 3)
复杂的使用方法和上述的IN是一样的,参考即可。

7)LIKE 查询

->where([
    'like', 'pack_name', '%sandbox%'
])
上面程式等同于`pack_name` LIKE '%sandbox%'
->where([
    'like', 'pack_name', [
        '%sandbox%',
        'com.famigo%'
    ]
])
上面程式等同于`pack_name` LIKE '%sandbox%' AND `pack_name` LIKE 'com.famigo%'

->where([
    'or like', 'pack_name', [
        '%sandbox%',
        'com.famigo%'
    ]
])
上面程式等同于`pack_name` LIKE '%sandbox%' OR `pack_name` LIKE 'com.famigo%'
->where([
    'or not like', 'pack_name', [
        '%sandbox%',
        'com.famigo%'
    ]
])
上面程式等同于`pack_name` NOT LIKE '%sandbox%' OR `pack_name` NOT LIKE 'com.famigo%'
->where([
    'not like', 'pack_name', [
        '%sandbox%',
        'com.famigo%'
    ]
])
上面程式等同于`pack_name` NOT LIKE '%sandbox%' AND `pack_name` NOT LIKE 'com.famigo%'

原文:
https://blog.csdn.net/liruxing1715/article/details/48575025

有时候我们需要在后台配置某个时间段做什么事情,那么人性化的配置一般是: 21:30-8:30 这样的配置,那么我们使用的时候怎么来判断当前的时间是否在这个时间段内呢

下面是我的一段参考代码,仅作参考:


$mp_lyEnable = '21-8:30'; //后台设置的时间段
$lyEnable = false; //客服是否在离线时段
if ($mp_lyEnable) {
    $mp_lyEnable = explode('-', $mp_lyEnable);
    if (count($mp_lyEnable) == 2) {
         @list($startHour, $startMinute) = explode(':', $mp_lyEnable[0]);
         @list($endHour, $endMinute) = explode(':', $mp_lyEnable[1]);
         if (is_null($startMinute)) $startMinute = 0;
         if (is_null($endMinute)) $endMinute = 59;
         $startTime = strtotime(sprintf(date('Y-m-d %\d:%\d:00'), $startHour, $startMinute));
         $endTime = strtotime(sprintf(date('Y-m-d %\d:%\d:00'), $endHour, $endMinute));
         if ($endHour < $startHour) {
             //跨天
             $endTime += 86400;
         }
         $now = time();
         if ($now > $startTime && $now < $endTime) $lyEnable = true;
         var_dump(date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', $endTime));
    }
}

注意这里使用的是yii 1.1版本
有时候我们要批量写入数据,同时还要去重复,如下的方法可能有用(数据库设置了唯一索引)

$this->db = new CDbConnection('mysql:host=dev.database.local;port=3306;dbname=xxx', 'xxx', 'xxx');

$data = [
    ['areaId'=>1, 'title'=>'text1'],
    ['areaId'=>2, 'title'=>'text2'],
    ['areaId'=>3, 'title'=>'text3'],
];

$builder = $this->db->schema->getCommandBuilder();
$command = $builder->createMultipleInsertCommand("{{ztags}}", $data);
$sql = $command->getText();
$sql = str_replace('INSERT INTO', 'INSERT IGNORE INTO', $sql);

$params = [];
foreach ($data as $i=>$d) {
    foreach ($d as $k=>$v) {
        $params[":{$k}_{$i}"] = $v;
    }
}

$this->db->createCommand($sql)->execute($params);

文档参考:

  1. https://www.yiiframework.com/doc/api/1.1/CDbCommandBuilder#createMultipleInsertCommand-detail
  2. https://www.yiiframework.com/doc/api/1.1/CDbCommand#getText-detail

从阿里的数据库RDB库mysqldump下来的文件里看到
SET @@GLOBAL.GTID_PURGED='ddd7ffa8-e648-11e7-bdc7-00163e04b878:1-72683802';

这样的东西,然后在自建的数据库(宝塔)中导入,报出了如下的错误
ERROR 1227 (42000) at line 18: Access denied; you need (at least one of) the SUPER privilege(s) for this operation

备份的时候也有提示:
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events.

当然你要导入备份文件的话,把报错那几行内容注掉可以正常导入的。

然后就好奇网上查了一下,看到这文章不错,这里记录一下:

https://blog.csdn.net/woailyoo0000/article/details/88981380