Mysql查询按最新时间从多列获取数据到选定数据

时间:2016-11-28 10:42:34

标签: php html css mysql ajax

我的数据库中有多个列但是当我在不同的列中插入数据时,我想要检索我现在插入数据的列的数据,并且不应该影响其他数据检索。

SELECT h1 FROM latestnews ORDER BY h1 DESC LIMIT 1

1 个答案:

答案 0 :(得分:0)

鉴于此表

drop table if exists mylog;
create table mylog 
(id int auto_increment primary key, dt date, tbl varchar(100),fld varchar(100), newvalue varchar(100));

和这张表

CREATE TABLE `bank_trtypes` (
  `ID` int(11) DEFAULT NULL,
  `Version` decimal(10,2) DEFAULT NULL,
  `launchdate` date NOT NULL,
  `cde` varchar(3) NOT NULL,
  `LongName` varchar(30) NOT NULL,
  `crdr` varchar(1) NOT NULL COMMENT 'C=credit,D=Debit,N=Note',
  `CrLedgerCde` int(11) DEFAULT NULL,
  `DRLedgerCde` int(11) DEFAULT NULL,
  `pbk` varchar(1) NOT NULL COMMENT 'y=print to passbook',
  `stmt` varchar(1) NOT NULL COMMENT 'y=print to stmt'
) ENGINE=InnoDB DEFAULT CHARSET=latin1

此触发器

drop trigger if exists `t_bank_trtypes_update`;
delimiter //

CREATE DEFINER=`root`@`localhost` TRIGGER `t_bank_trtypes_update` after UPDATE ON `bank_trtypes` FOR EACH ROW begin

    if new.launchdate <> old.launchdate then    insert into mylog (dt , tbl,fld , newvalue) values (now(),'bank_trtypes','launchdate',new.launchdate); end if;
    if new.cde <> old.cde then insert into mylog (dt , tbl,fld , newvalue) values (now(),'bank_trtypes','cde',new.cde); end if;
    if new.longname <> old.longname then    insert into mylog (dt , tbl,fld , newvalue) values (now(),'bank_trtypes','longname',new.longname); end if;
    if new.crledgercde <> old.crledgercde then  insert into mylog (dt , tbl,fld , newvalue) values (now(),'bank_trtypes','crledgercde',new.crledgercde); end if;
    if new.drledgercde <> old.drledgercde then  insert into mylog (dt , tbl,fld , newvalue) values (now(),'bank_trtypes','drledgercde',new.drledgercde); end if;
    if new.pbk <> old.pbk then  insert into mylog (dt , tbl,fld , newvalue) values  (now(),'bank_trtypes','pbk',new.pbk); end if;

end //

delimiter ;

在更新中生成(1个或多个)mylog条目 像这样

MariaDB [sandbox]> select * from bank_trtypes;
+------+---------+------------+-----+----------+------+-------------+-------------+-----+------+
| ID   | Version | launchdate | cde | LongName | crdr | CrLedgerCde | DRLedgerCde | pbk | stmt |
+------+---------+------------+-----+----------+------+-------------+-------------+-----+------+
|    1 |    2.00 | 2016-01-01 | so  | Stand    | d    |        NULL |        NULL | y   | y    |
+------+---------+------------+-----+----------+------+-------------+-------------+-----+------+
1 row in set (0.00 sec)

MariaDB [sandbox]> select * from mylog;
Empty set (0.00 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> update bank_trtypes set pbk = 'n' where 1 = 1;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [sandbox]>
MariaDB [sandbox]> select * from mylog;
+----+------------+--------------+------+----------+
| id | dt         | tbl          | fld  | newvalue |
+----+------------+--------------+------+----------+
|  1 | 2016-11-28 | bank_trtypes | pbk  | n        |
+----+------------+--------------+------+----------+
1 row in set (0.00 sec)

尝试谷歌搜索MYSQL更新触发器以获取更多示例。