mysql表的設計 mysql,表設計

2021-10-18 11:56:47 字數 3184 閱讀 7528

閒著沒事搞了一下,歡迎指教。。

使用者表:

create table `usr` (

`uid` int(11) not null,

`name` char(10) default null,

primary key (`uid`)

) engine=innodb default charset=utf8;

吃飯記錄表:

create table `meal` (

`mid` int(11) not null,

`money` int(11) default null comment '付款金額',

`pay_uid` int(11) default null comment '付款人員id',

`dt` date default null,

primary key (`mid`)

) engine=innodb default charset=utf8;

吃飯人員流水表:

create table `meal_jnl` (

`id` int(11) not null auto_increment,

`mid` int(11) default null,

`in_uid` int(11) default null comment '參加人員id',

primary key (`id`)

) engine=innodb auto_increment=52 default charset=utf8;

造資料:

insert into `usr` (`uid`, `name`)

values

(1, 'jim'),

(2, 'jeak'),

(3, 'lucy'),

(4, 'carl'),

(5, 'jerry'),

(6, 'mark');

insert into `meal` (`mid`, `money`, `pay_uid`, `dt`)

values

(1, 122, 2, '2017-06-01'),

(2, 56, 4, '2017-06-01'),

(3, 56, 1, '2017-06-02'),

(4, 76, 3, '2017-06-03'),

(5, 54, 5, '2017-06-04'),

(6, 66, 2, '2017-06-05'),

(7, 77, 2, '2017-06-05'),

(8, 34, 3, '2017-06-06'),

(9, 54, 1, '2017-06-07'),

(10, 77, 4, '2017-06-08'),

(11, 45, 5, '2017-06-08'),

(12, 87, 2, '2017-06-10'),

(13, 123, 3, '2017-06-11'),

(14, 431, 1, '2017-06-11'),

(15, 23, 4, '2017-06-12');

insert into `meal_jnl` (`id`, `mid`, `in_uid`)

values

(1, 1, 2),

(2, 1, 3),

(3, 1, 4),

(4, 1, 5),

(5, 2, 1),

(6, 2, 2),

(7, 2, 4),

(8, 2, 5),

(9, 3, 1),

(10, 3, 2),

(11, 3, 3),

(12, 3, 4),

(13, 3, 5),

(14, 4, 3),

(15, 4, 4),

(16, 4, 5),

(17, 5, 2),

(18, 5, 5),

(19, 6, 4),

(20, 6, 5),

(21, 6, 1),

(22, 6, 2),

(23, 7, 2),

(24, 7, 5),

(25, 7, 1),

(26, 8, 2),

(27, 8, 3),

(28, 8, 4),

(29, 8, 5),

(30, 9, 1),

(31, 9, 4),

(32, 10, 1),

(33, 10, 2),

(34, 10, 3),

(35, 10, 4),

(36, 10, 5),

(37, 11, 1),

(38, 11, 2),

(39, 11, 5),

(40, 12, 2),

(41, 12, 5),

(42, 13, 3),

(43, 13, 1),

(44, 14, 1),

(45, 14, 3),

(46, 14, 4),

(47, 15, 3),

(48, 15, 4),

(49, 15, 5),

(50, 15, 6),

(51, 11, 6);

獲取沒人月底應付金額:

select in_uid,out_mon-pay_mon tm,name from (

select sum(case when money is null then 0 else money end) pay_mon,uid,name

from meal right join usr

on meal.`pay_uid`=usr.uid

where dt between '' and ''

group by pay_uid) aaa

right join (

select in_uid,sum(am) out_mon

from (

select a.mid,money/count(in_uid) am

from meal_jnl a join meal b

on a.mid=b.mid

where b.dt between '' and ''

group by mid) aa

join meal_jnl bb

on aa.mid=bb.mid group by in_uid) bbb

on aaa.uid=bbb.in_uid;

mysql設計表月份 mysql,表設計

閒著沒事搞了一下,歡迎指教。使用者表 create table usr uid int 11 not null,name char 10 default null,primary key uid engine innodb default charset utf8 吃飯記錄表 create tabl...

mysql豎向表設計 mysql,表設計

閒著沒事搞了一下,歡迎指教。使用者表 create table usr uid int 11 not null,name char 10 default null,primary key uid engine innodb default charset utf8 吃飯記錄表 create tabl...

mysql 團購表設計 MySQL表設計規則

1 表設計之欄位 1 表 列名必須有注釋 2 命名必須規範,由數字 字母和 組成,不能使用空格,不能使用關鍵字type status等 3 命名長度不超過20 4 列舉型別字段狀態字段,使用char 1 或者char 2 型別,填寫預設值,狀態字段的預設值不能為null,一般是設定成0或 1,而且注...