简体   繁体   English

使用php在sql中找到最新条目的列

[英]Find the column with the newest entry in sql using php

Using a php code, I want to know in what column the latest entry is located if I for instance is interested in id 2. Then column 'time4' with the time "2016-03-01 10:01:01" is the newest entry.使用 php 代码,如果我对 id 2 感兴趣,我想知道最新条目位于哪一列。然后时间为“2016-03-01 10:01:01”的“time4”列是最新的入口。

id      time1                   time2                   time3                   time4
1       2016-01-01 09:27:24     2016-01-02 10:01:01     2016-01-02 17:05:07     2016-01-01 10:01:01
2       2016-01-02 09:27:24     2016-01-03 10:01:01     2016-02-02 17:05:07     2016-03-01 10:01:01
3       2016-01-02 10:27:24     2016-01-03 11:01:01     2016-02-02 18:05:07     2016-03-01 11:01:01

I tried this我试过这个

<?php
$User=1;
$result = $con->query("SELECT MAX(TimeToSelect) as Output
FROM (
    SELECT time1 as TimeToSelect,id
    FROM tableTime
    union
    SELECT time2,id
    FROM tableTime
    union
    SELECT time3,id
    FROM tableTime
        union
    SELECT time4,id
    FROM tableTime
    ) as A
  WHERE id = '$User'");
$row = $result->fetch_array(MYSQLI_BOTH);

echo $something=$row[0];
?>

But then it echoes the date "2016-03-01 10:01:01" and not the column name "time4".但随后它与日期“2016-03-01 10:01:01”相呼应,而不是列名“time4”。

If you are only contains 4 columns to compare, you can use the below SQL to get what you want.如果你只包含 4 列进行比较,你可以使用下面的 SQL 来获得你想要的。
You can change the id in where condition to get the latest time is entered for particular 'id'.您可以在 where 条件中更改 id 以获取为特定“id”输入的最新时间。 (Each time each id) (每次每个id)

SELECT MAX(TimeToSelect) as Output
FROM (
    SELECT time1 as TimeToSelect,id
    FROM tableTime
    union
    SELECT time2,id
    FROM tableTime
    union
    SELECT time3,id
    FROM tableTime
    union
    SELECT time4,id
    FROM tableTime
    ) as A
  WHERE id = 1

If you want to get the columns name, try below SQL:如果要获取列名,请尝试以下 SQL:

SELECT ColumnName
FROM (
    SELECT time1 AS TimeToSelect,id,'time1' AS ColumnName
    FROM tableTime
    UNION
    SELECT time2,id,'time2'
    FROM tableTime
    UNION
    SELECT time3,id,'time3'
    FROM tableTime
    UNION
    SELECT time4,id,'time4' 
    FROM tableTime
    ) AS A 
WHERE id = 1 
AND TimeToSelect = (SELECT MAX(TimeToSelect)
                    FROM (
                         SELECT time1 AS TimeToSelect,id,'time1' AS ColumnName
                         FROM tableTime
                         UNION
                         SELECT time2,id,'time2'
                         FROM tableTime
                         UNION
                         SELECT time3,id,'time3'
                         FROM tableTime
                         UNION
                         SELECT time4,id,'time4' 
                         FROM tableTime
                         ) AS A
                    WHERE id = 1
                    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM