mysql数据库动态查询_数据库@MySQL常用SQL查询

查询表结构语句

select

column_name 列名,

column_type 数据类型,

data_type 字段类型,

character_maximum_length 长度,

is_nullable 是否为空,

column_default 默认值,

column_comment 备注

from

information_schema.columns

where

table_schema = 'plcs_web' -- 数据库名称

and table_name = 'wsxd_case' -- 表名

结果:

ebe658a7926ca6d84c7b93f6d73f4d45.png

实现一行变多行

实现多行变一行

根据自身状态更新

根据自身状态删除

触发器

存储过程基础

存储过程循环(传入字符串数组)

-- 建表语句

CREATE TABLE test (

id int(11) NOT NULL AUTO_INCREMENT,

letter varchar(255) DEFAULT NULL,

number varchar(255) DEFAULT NULL,

constant varchar(255) DEFAULT NULL,

other varchar(255) DEFAULT NULL,

PRIMARY KEY (id)

) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

-- 插入数据

insert into test

(letter, number, constant, other)

values

('a', '1', '甲', '金'),

('b', '2', '乙', '木'),

('c', '3', '丙', '水'),

('d', '4', '丁', '火'),

('a', '2', '甲', '土'),

('b', '3', '丙', '金'),

('c', '4', '乙', '木'),

('d', '4', '丁', '水'),

('e', '1', '丙', '火'),

('e', '3', '甲', '金');

select * from test;

-- 根据letter分组,再根据number倒序排序,取每个letter组的第2位

-- 查询语句

select *

from test a

order by a.letter, number desc

-- 看出结果

idletternumberconstantother

1a1甲金

2b2乙木

3c3丙水

8d4丁水

9e1丙火

;

-- 是用变量的方式,可以写出语句

select

a.id, a.letter, a.number, a.constant, a.other

from

(

select

if(@letter != a.letter, @rownum := 1, @rownum := @rownum + 1) as var_rownum,

@letter := a.letter as var_letter,

a.id,

a.letter,

a.number,

a.constant,

a.other

from test a

order by a.letter, a.number desc

) a

where a.var_rownum = 2;

select *

from test a

left join (

select

a.id, a.letter, a.number, a.constant, a.other

from

(

select

if(@letter != a.letter, @rownum := 1, @rownum := @rownum + 1) as var_rownum,

@letter := a.letter as var_letter,

a.id,

a.letter,

a.number,

a.constant,

a.other

from test a

order by a.letter, a.number desc

) a

where a.var_rownum = 2

) b

on a.id = b.id

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值