思路:

将table分为两个表,表头部分和内容部分,通过jquery实时控制上下两表的th,td宽度统一,在表头部分触顶时改变定位方式使它悬浮

代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!DOCTYPE html>
<html>
<head>
<title>悬浮table头</title>
<style type="text/css">
th{background: green}
th,td{line-height: 200px;border: 1px solid #eee;width: 200px;}
.table{word-break:break-all;word-wrap:break-word}
</style>
</head>
<body>
<div style="width: 100%;height: 50px;background: yellow;">导航栏</div>
<table id="header_table" style="top: 0;z-index: 1000;margin-bottom: 0px;" class="table">
<thead id="t_thead">
<tr>
<th>标题一</th>
<th>标题二</th>
<th>标题三</th>
<th>标题四</th>
<th>标题五</th>
</tr>
</thead>
</table>
<table id="body_table" class="table">
<tbody>
<tr><td>内容一</td><td>内容二</td><td>内容三</td><td>内容四</td><td>内容五</td></tr>
<tr><td>内容一</td><td>内容二</td><td>内容三</td><td>内容四</td><td>内容五</td></tr>
<tr><td>内容一</td><td>内容二</td><td>内容三</td><td>内容四</td><td>内容五</td></tr>
<tr><td>内容一</td><td>内容二</td><td>内容三</td><td>内容四</td><td>内容五</td></tr>
<tr><td>内容一</td><td>内容二</td><td>内容三</td><td>内容四</td><td>内容五</td></tr>
<tr><td>内容一</td><td>内容二</td><td>内容三</td><td>内容四</td><td>内容五</td></tr>
<tr><td>内容一</td><td>内容二</td><td>内容三</td><td>内容四</td><td>内容五</td></tr>
<tr><td>内容一</td><td>内容二</td><td>内容三</td><td>内容四</td><td>内容五</td></tr>
<tr><td>内容一</td><td>内容二</td><td>内容三</td><td>内容四</td><td>内容五</td></tr>
</tbody>
</table>
</body>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
// 调整上下表宽度统一
var tableHd = $("#header_table");
var tableTop = tableHd.offset().top;
$(window).scroll(function(){
var _t = $(window).scrollTop();

if(_t-tableTop>=0){
//table-header 已经到窗口顶部了
tableHd.css({
"position":"fixed"
})
}else{
tableHd.css({
"position":"static"
})
}
});
autoWidth();
function autoWidth(){
var body_first_tr = $('#body_table').width();
$('#header_table').width(body_first_tr);


for(var i=0;i<5;i++){
var header_id = '#body_table tbody tr:eq(0) td:eq('+i+')';
var body_id = '#header_table thead tr:eq(0) th:eq('+i+')';
var body_first_tr = $(header_id).width();
$(body_id).width(body_first_tr);
}
//console.log(body_first_tr);
}
$(window).resize(function () {//当浏览器大小变化时
autoWidth();
});
</script>
</html>

思路:

将题目拆分成两部分
1先查出所有在a日登录过的用户名
2用1的结果加上b日的时间为条件完成最终查询

具体代码

假设:
日志表为:login_log
用户名为:username
登录时间字段为:time
时间a为:time_a
时间b为:time_b
1的代码

1
SELECT username from login_log where time = 'time_a' GROUP BY username;

2最终代码

1
SELECT username FROM login_log WHERE username IN (SELECT username from login_log where time = 'time_a' GROUP BY username) AND time <> 'time_b' GROUP BY username;

  1. 选择有效率的表名顺序
  2. where子句的字段顺序(个人理解:如查询用户表的20岁男性数据,条件应该先写年龄再写性别,因为通常情况下指定年龄的数据更少可以先过滤掉大多数无用数据从而提高查询的效率)
  3. 查询语句中避免使用’*‘,最好需要什么就查询什么字段
  4. 用where子句替换having子句
  5. 避免在索引列上使用计算
  6. 提高group by的语句效率(提前用条件过滤掉不需要的数据)
  7. 尽量避免将字段的默认值设置为null
  8. 关联查询替代子查询
  9. 尽量少使用like关键字和通配符
  10. 使用事物和外键
  11. 建立索引
  12. 使用适合的字段属性(尽可能的使用int或varchar类型并设置合理的长度)(一些状态位要用int类型标识并且大多都不适宜建立索引)
  13. 读写分离主从复制(建立主数据库和若干个从数据库,当进行数据库操作时判断操作是读取或者其他操作,如果是读则在从数据库中查询数据,如果是其他操作则在主数据库中进行,然后通过异步的主从复制将数据同步到从数据库)

删除掉某表重名用户

表名: user
字段:
主键: id
用户名: name

1
delete from user where id not in(SELECT max(id) from user group by name);

本功能同样适用于其他的框架或者原生的程序(需要根据实际情况做修改)

1.本功能参考地址:https://www.cnblogs.com/xiuber/p/5945597.html
2.本功能必须先引入jquery和jquery-ui(注意引入先后顺序)

前端代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'uuid',
[
// 添加一个隐藏的uuid为要修改数据的主键方便获取(其实这个并没有用到只是以防万一的备选方案)
'attribute' => 'uuid',
'contentOptions' => [
'class' => 'uuid',
],
'headerOptions' => ['style'=>'display:none'],
'contentOptions' => ['style'=>'display:none'],
],
'name',
[
'attribute' => 'order_num',//这里是排序的字段
'contentOptions' => [
'class' => 'index',//向td中插入class方便后续修改排序的显示
],
],
],
]); ?>
<script type="text/javascript">
var old_array = new Array();
var paixu_array = new Array();
$(document).ready(function(){
var fixHelperModified = function(e, tr) {
var $originals = tr.children();
var $helper = tr.clone();
$helper.children().each(function(index) {
//获取宽度
$(this).width($originals.eq(index).width())
});
return $helper;
},
updateIndex = function(e, ui) {
$('td.index', ui.item.parent()).each(function (i) {
//console.log($(this).parent().attr('data-key'));
// 获取数据的主键data-key这个属性为GridView自动生成的 如果使用备选方案可以这么写console.log($(this).parent().find('td').eq(2).html());
var uuid = $(this).parent().attr('data-key');
// 将排序的顺序和主键存入数组
paixu_array[i + 1] = uuid;
// 修改表格的排序显示
$(this).html(i + 1);
// 调用方法将数据传入控制器
save_paixu();
});
};
// #paixu_div是包裹表格的一层div这里没有写
$("#paixu_div tbody").sortable({
helper: fixHelperModified,
stop: updateIndex
}).disableSelection();
});
function save_paixu() {
// 获取有多少条数据
var tr_num = $(".ui-sortable tr").length;
tr_num = tr_num+1;
if (paixu_array.length != tr_num){
// 数组的长度要等于数据的长度+1(原因自己看数据的打印结果)
return false;
}else {
if (paixu_array.toString() == old_array.toString()){
// 比较旧的排序于新的排序有没有改变
return false;
}
}
// 这个判断没有用忘记删了
if(1) {
new__array = paixu_array;
paixu_array = [];
$.ajax({
url: "ajax地址",
data: {"paixu_array": new__array},
type: "post",
success: function (backdata) {
//console.log(backdata);
var res = jQuery.parseJSON(backdata);
if(res.success) {
//alert(res.message);
old_array = paixu_array;
paixu_array = [];
}else {
alert(res.message);
}

}, error: function (error) {
console.log(error);
}
});
}

}
</script>
控制器代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
protected function findModel($id)
{
if (($model = ModelName::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
public function actionSavePaixu(){
if(Yii::$app->request->isAjax && isset($_POST['paixu_array'])) {
$paixu_array = $_POST['paixu_array'];
//事物
$transation = Yii::$app->db->beginTransaction();
try{
foreach($paixu_array as $k => $v)
{
if ($k){
$model = $this->findModel($v);
$model->order_num = $k;
$model->save();
}
}
// 执行事务
$transation->commit();
echo json_encode(array('success'=>true,'message'=>'修改成功'));
}catch(\Exception $e){
// 回滚
$transation->rollBack();
// 记录错误
echo json_encode(array('success'=>FALSE,'message'=>'修改失败'));
}
}
}
以上代码仅供参考要注重理解含义举一反三,完全复制粘贴是不可能实现的

曾经针对文本的换行添加样式的简单方法,记一下笔记
1
2
3
4
5
6
7
8
public static function add_html_p($text){
$keyword_arr = explode("\n", $text);
$new_text = '';
foreach ($keyword_arr as $k => $v){
$new_text .= '<p style="text-indent:2em;margin: 0px;line-height:1.7">'.$v.'</p>';
}
return $new_text;
}