<db> order by 时sqlserver认为null是最大值

本文探讨了SQL Server与Oracle数据库中NULL值排序的不同处理方式。SQL Server默认视NULL为最小值,而Oracle则视其为最大值。文中详细介绍了两种数据库中NULL排序的具体规则及调整方法。
order by时sqlserver和oracle对null值的对待相反。

[b]【sqlserver】[/b]:

sqlserver 认为 null 最小。

升序排列:null 值默认排在最前。

要想排后面,则:order by case when col is null then 1 else 0 end ,col

降序排列:null 值默认排在最后。

要想排在前面,则:order by case when col is null then 0 else 1 end , col desc

[b]【oracle】:[/b]

oracle认为 null 最大。

升序排列,默认情况下,null值排后面。

降序排序,默认情况下,null值排前面。

有几种办法改变这种情况:

(1)用 nvl 函数或decode 函数 将null转换为一特定值

(2)用case语法将null转换为一特定值(oracle9i以后版本支持。和sqlserver类似):
order by (case mycol when null then ’北京漂客’ else mycol end)

(3)使用nulls first 或者nulls last 语法。

这是oracle专门用来null值排序的语法。

nulls first :将null排在最前面。如:select * from mytb order by mycol nulls first

nulls last :将null排在最后面。如:select * from mytb order by mycol nulls last

转[url]http://blog.sina.com.cn/s/blog_69d5d7130100ubsp.html[/url]

(4)在Sql Server 中使用case when then 判断某字段是否为null,和判断是否为字符或数字时的写法不一样,如果不注意,很容易搞错

错误方法:
CASE columnName WHEN null THEN 0 ELSE columnName END

正确方法:
CASE WHEN columnName is null THEN 0 ELSE columnName END
人进商城管理中心 0.00 今日销售总额 0 今日订单数 0 今日注册会员 0 今日入驻店铺数 0 / 8 待审核/店铺总数 待处理 936 个 待处理佣金 52 个 待审核商品 2 个 会员充值 4 个 会员提现 1 个 会员留言 725 个 商品评论 148 个 用户晒单 4 个 标签审核 0 个 商品 220 件 自营商品总数 115 件 入驻商商品总数 105 件 库存警告商品数 57 件 新品推荐数 110 件 精品推荐数 48 件 热销商品数 24 件 促销商品数 0 件 已下架商品总数 73 件 订单 39 笔 待发货订单 0 笔 待支付订单 0 笔 待确认订单 0 笔 部分发货订单 0 笔 退款申请 170 笔 退货申请 5 笔 新缺货登记 0 笔 已成交订单数 2 笔 订单来源统计 订单排行统计 销售额统计 系统信息 服务器操作系统: Linux (192.168.0.142) Web 服务器: nginx/1.20.2 PHP 版本: 5.6.40 MySQL 版本: 5.7.42-log 安全模式: 否 安全模式GID: 否 Socket 支持: 是 区设置: PRC GD 版本: GD2 ( JPEG GIF PNG) Zlib 支持: 是 IP 库版本: 20071024 文件上传的最大大小: 100M 人进商城1.0版 安装日期: 2017-05-18 编码: UTF-8 人进商城: szrengjing.com 强烈建议您将data/config.php文件属性设置为644(linu/unix)或只读权限(WinNT) 强烈建议您在网站上线之后将后台入口目录admin重命名,可增加系统安全性 请注意定期做好数据备份,数据的定期备份可最大限度的保障您网站数据的安全 人进商城: <?php define('IN_ECS', true); require_once('../includes/init.php'); // 检查管理员权限 admin_priv('users_manage'); // 需要用户管理权限 // 连接数据库 $pdo = new PDO("mysql:host=localhost;dbname=szrengjing_com", "szrengjing_com", "ZparETNy4DTZBAiT"); $pdo->exec("SET NAMES utf8"); // 获取搜索参数 $user_id = $_GET['user_id'] ?? null; $start_date = $_GET['start_date'] ?? date('Y-m-01'); // 默认本月开始 $end_date = $_GET['end_date'] ?? date('Y-m-d H:i:s'); // 查询语句 $sql = "SELECT l.*, u.username FROM ecs_user_credit_log l LEFT JOIN ecs_users u ON l.user_id = u.id WHERE 1=1"; $params = []; if ($user_id) { $sql .= " AND l.user_id = ?"; $params[] = $user_id; } if ($start_date) { $sql .= " AND l.created_at >= ?"; $params[] = $start_date . ' 00:00:00'; } if ($end_date) { $sql .= " AND l.created_at <= ?"; $params[] = $end_date . ' 23:59:59'; } $sql .= " ORDER BY l.created_at DESC LIMIT 100"; $stmt = $pdo->prepare($sql); $stmt->execute($params); $logs = $stmt->fetchAll(PDO::FETCH_ASSOC); ?> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8" /> <title>信用日志管理后台</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" /> </head> <body class="p-4"> <h2 class="mb-4">📊 用户信用变更日志</h2> <!-- 搜索表单 --> <form method="get" class="mb-4 bg-light p-3 rounded"> <div class="row"> <div class="col-md-3"> <label>用户ID:</label> <input type="number" name="user_id" class="form-control" value="<?= htmlspecialchars($user_id) ?>" /> </div> <div class="col-md-3"> <label>起始日期:</label> <input type="date" name="start_date" class="form-control" value="<?= $start_date ? date('Y-m-d', strtotime($start_date)) : '' ?>" /> </div> <div class="col-md-3"> <label>结束日期:</label> <input type="datetime-local" name="end_date" class="form-control" value="<?= $end_date ? date('Y-m-d\TH:i', strtotime($end_date)) : '' ?>" /> </div> <div class="col-md-3 d-flex align-items-end"> <button type="submit" class="btn btn-primary">查询</button> </div> </div> </form> <!-- 日志表格 --> <table class="table table-striped table-hover"> <thead> <tr> <th>ID</th> <th>用户ID</th> <th>用户名</th> <th>旧分值</th> <th>新分值</th> <th>变化值</th> <th>原因</th> <th>间</th> </tr> </thead> <tbody> <?php foreach ($logs as $log): ?> <tr> <td><?= $log['id'] ?></td> <td><?= $log['user_id'] ?></td> <td><?= htmlspecialchars($log['username'] ?? '未知') ?></td> <td><?= $log['old_score'] ?></td> <td><?= $log['new_score'] ?></td> <td class="<?= $log['change_value'] > 0 ? 'text-success' : ($log['change_value'] < 0 ? 'text-danger' : '') ?>"> <?= $log['change_value'] ?> </td> <td><?= htmlspecialchars($log['reason']) ?></td> <td><?= $log['created_at'] ?></td> </tr> <?php endforeach; ?> </tbody> </table> <?php if (empty($logs)): ?> <p class="text-muted text-center">🔍 没有找到符合条件的记录。</p> <?php endif; ?> </body> </html>
最新发布
11-10
using System.Text.Json; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; using NySchedulingSystem.Domain.Aggregates; using NySchedulingSystem.Domain.Entities; namespace NySchedulingSystem.Infrastructure.Persistence.Configurations { public class ProductionPlanEntityTypeConfiguration : IEntityTypeConfiguration<ProductionPlan> { public void Configure(EntityTypeBuilder<ProductionPlan> builder) { // 数据库表名 builder.ToTable("production_plans"); // 主键配置 builder.HasKey(x => x.id); builder.Property(x => x.id).HasColumnName("id"); // 值对象列映射 builder.Property<int>("production_code").HasColumnName("production_code"); builder.Property<string>("work_order_code").HasColumnName("work_order_code"); builder.Property<string>("drawing_code").HasColumnName("drawing_code"); builder.Property<string>("dispatcher").HasColumnName("dispatcher"); builder.Property<int>("plan_count").HasColumnName("plan_count"); builder.Property<DateTime>("plan_begin_time").HasColumnName("plan_begin_time"); builder.Property<DateTime>("plan_end_time").HasColumnName("plan_end_time"); // 集合关系配置 builder.OwnsMany<Procs>("procses", procs_builder => { // 数据库表名 procs_builder.ToTable("procs"); // 主键配置 procs_builder.HasKey(x => x.id); procs_builder.Property(x => x.id).HasColumnName("id"); // 值对象列映射 procs_builder.Property<string>("procs_code").HasColumnName("procs_code"); procs_builder.Property<string>("device_code").HasColumnName("device_code"); procs_builder.Property<string>("device_type").HasColumnName("device_type"); procs_builder.Property<string>("priority").HasColumnName("priority"); procs_builder.Property<string>("team_leader").HasColumnName("team_leader"); procs_builder.Property<string>("worker").HasColumnName("worker"); procs_builder.Property<DateTime>("plan_begin_time").HasColumnName("plan_begin_time"); procs_builder.Property<DateTime>("plan_end_time").HasColumnName("plan_end_time"); procs_builder.OwnsMany<DerivedProcs>("derived_procses", derived_procs_builder => { // 数据库表名 derived_procs_builder.ToTable("derived_procs"); // 主键配置 derived_procs_builder.HasKey(x => x.id); derived_procs_builder.Property(x => x.id).HasColumnName("id"); // 值对象列映射 derived_procs_builder.Property<string>("priority").HasColumnName("priority"); derived_procs_builder.Property<string>("device_type").HasColumnName("device_type"); derived_procs_builder.Property<string>("device_code").HasColumnName("device_code"); derived_procs_builder.Property<string>("plan_count").HasColumnName("plan_count"); derived_procs_builder.Property<string>("pallet_count").HasColumnName("pallet_count"); derived_procs_builder.Property<DateTime>("plan_begin_time").HasColumnName("plan_begin_time"); derived_procs_builder.Property<DateTime>("plan_end_time").HasColumnName("plan_end_time"); derived_procs_builder.Property<DateTime?>("actual_start_time").HasColumnName("actual_start_time"); derived_procs_builder.Property<DateTime?>("actual_end_time").HasColumnName("actual_end_time"); derived_procs_builder.Property<int>("state").HasColumnName("state"); derived_procs_builder.Property<string>("team_leader").HasColumnName("team_leader"); derived_procs_builder.Property<string>("worker").HasColumnName("worker"); derived_procs_builder.OwnsMany<DerivedProcsTask>("tasks", derived_procs_task_builder => { // 数据库表名 procs_builder.ToTable("derived_procs_task"); // 主键配置 derived_procs_task_builder.HasKey(x => x.id); derived_procs_task_builder.Property(x => x.id).HasColumnName("id"); // 值对象列映射 derived_procs_task_builder.Property<DateTime>("create_time").HasColumnName("create_time"); derived_procs_task_builder.Property<DateTime?>("start_time").HasColumnName("start_time"); derived_procs_task_builder.Property<DateTime?>("end_time") .HasColumnName("end_time"); derived_procs_task_builder.Property<List<string>>("pallet_codes") .HasColumnName("pallet_codes") .HasConversion( x => JsonSerializer.Serialize(x, JsonOptions), x => JsonSerializer.Deserialize<List<string>>(x, JsonOptions) ?? new()); }); }); }); } static readonly JsonSerializerOptions JsonOptions = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = false }; } } 怎么自动生成数据库
09-24
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值