mysql拾零

1、 $sql='select * ,count(distinct newType) from news group by newType';
//查询各类新闻中的第一条值
原理: 先按newType字段分组,然后用count()统计每一组中的记录数——注意,这里巧妙地运用了count()函数会返回一条记录,而且是相同类型中的第一条记录的特点。


2、 mysql去除查询结果重复值
distinct过滤多余的重复记录只保持一条,但往往只用它来返回不重复的记录的条数,而不是用它来返回不重记录的所有值。
distinct只能返回它的目标字段,而无法返回其他字段

例子:
id name
1 a
2 b
3 c
4 c
5 b
要求: 查询某列name不重复的所有数据
select distinct name from table
那若要得到id呢?
select distinct name,id from table
这样子查询出的结果为:
1 a
2 b
3 c
4 c
5 b
因为distinct相当于同时作用了两个字段,也即只有id与name都相同时才会被排除

解决方案:
A、 使用group_concat(distinct name)配合group by name
B、 select * , count(distinct name) from table group by name
C、 select id, name from table group by name


3、 如何判断数据库中的一个表是否有更新?
触发器, 给表增加timestamp字段

4、 mysql中的关联查询
left join 显示所有坐标,右表显示(关联字段在左表中出现)的记录
right join 反之
inner join 显示左右表(关联字段在左右表中都出现的)


5、mysql中limit字段的使用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java代码实现: ```java public class NumberToChinese { // 数字转中文大写 private static final String[] CHINESE_NUMBERS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; private static final String[] CHINESE_UNITS = {"", "拾", "佰", "仟"}; private static final String[] CHINESE_GROUP_UNITS = {"", "万", "亿"}; public static String numberToChinese(double num) { if (num == 0) { return "零元整"; } long integerPart = (long) num; int decimalPart = (int) Math.round((num - integerPart) * 100); StringBuilder sb = new StringBuilder(); int groupIndex = 0; while (integerPart > 0) { int groupNum = (int) (integerPart % 10000); sb.insert(0, CHINESE_GROUP_UNITS[groupIndex++]); if (groupIndex > 1 && groupNum < 1000) { sb.insert(0, "零"); } sb.insert(0, groupToChinese(groupNum)); integerPart /= 10000; } sb.append("元"); if (decimalPart > 0) { sb.append(decimalToChinese(decimalPart)); } else { sb.append("整"); } return sb.toString(); } private static String groupToChinese(int num) { StringBuilder sb = new StringBuilder(); int unitIndex = 0; boolean addZero = false; while (num > 0) { int digit = num % 10; if (digit == 0) { addZero = true; } else { if (addZero) { sb.insert(0, CHINESE_NUMBERS[0]); } sb.insert(0, CHINESE_NUMBERS[digit] + CHINESE_UNITS[unitIndex]); addZero = false; } unitIndex++; num /= 10; } return sb.toString(); } private static String decimalToChinese(int num) { StringBuilder sb = new StringBuilder(); int digit = num / 10; if (digit > 0) { sb.append(CHINESE_NUMBERS[digit] + "角"); } digit = num % 10; if (digit > 0) { sb.append(CHINESE_NUMBERS[digit] + "分"); } return sb.toString(); } public static void main(String[] args) { double num = 12345678; String chinese = numberToChinese(num); System.out.println(chinese); } } ``` 输出结果为:`壹仟贰佰叁拾肆万伍仟陆佰柒拾捌元整`

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值