简体   繁体   English

如何从多个表中按最新日期记录排序?

[英]How to order by latest date record from multiple tables?

This is my full code: 这是我的完整代码:

<?php $sid = $_SESSION['userid'];
$stmt = $pdo->prepare("
    SELECT tableA.*, tableB.*, tableC.*
    FROM tableA
    LEFT JOIN tableB
    ON tableA.tableAuserid = tableB.tableAuserid
    LEFT JOIN tableC
    ON tableA.tableAuserid = tableC.tableCuserid
    WHERE tableA.tableAuserid = ?
    ;
");
$stmt->bindParam(1, $sid, PDO::PARAM_INT);
$stmt->execute();
$columns = $stmt->fetchAll();

foreach($columns as $column):
$tableAdate = $column['tableAdate'];
$tableBdate = $column['tableBdate'];
$tableCdate = $column['tableCdate'];
    if ($tableAdate): ?>
        <div><?php echo $tableAdate; ?></div>
    <?php endif;
    if ($tableBdate): ?>
        <div><?php echo $tableBdate; ?></div>
    <?php endif;
    if ($tableCdate): ?>
        <div><?php echo $tableCdate; ?></div>
    <?php endif;
endforeach; ?>

My 3 database tables are: tableA : 我的3个数据库表是: tableA

tableAid | tableAuserid | tableAdate
    1    |     44       | 2018-07-13 11:09:11
    2    |     44       | 2019-05-23 01:21:29

tableB : tableB

tableBid | tableBuserid | tableBdate
    1    |     44       | 2019-08-11 17:41:01

And tableC : tableC

tableCid | tableCuserid | tableCdate
    1    |     44       | 2014-03-14 09:18:16

This is the result that's currently being echoed from my code: 这是我的代码当前正在回显的结果:

2018-07-13 11:09:11
2019-08-11 17:41:01
2014-03-14 09:18:16
2019-05-23 01:21:29
2019-08-11 17:41:01
2014-03-14 09:18:16

But this is the result I want achieved from the code: 但这是我想要从代码中获得的结果:

2019-08-11 17:41:01 (tableB)
2019-05-23 01:21:29 (tableA)
2018-07-13 11:09:11 (tableA)
2014-03-14 09:18:16 (tableC)

Basically each record must be retrieved with no duplicate values, result must be determined by the $_SESSION['userid'] variable, ORDER arrangement of SQL code must be by the latest date DESC of all dates combined from all 3 tables. 基本上,每条记录都必须检索没有重复的值,结果必须由$ _SESSION ['userid']变量确定,SQL代码的ORDER排列必须由所有3个表中所有日期的最新日期DESC决定。 How would I accomplish this? 我将如何完成?

Feel like I am missing some nuance of the question but perhaps not. 感觉我在想这个问题,但可能没有。 Lot's of ways to accomplish this. 有很多方法可以做到这一点。

The following UNION allows for a fair amount of flexibility, that is, the ability to refine criteria per table and still sort on the SQL side of things. 下面的UNION提供了相当大的灵活性,即,能够优化每个表的条件并仍然在SQL方面进行排序。

SELECT tId, tUserId, tDate FROM tableA WHERE tUserId = ?
UNION ALL 
SELECT tId, tUserId, tDate FROM tableB WHERE tUserId = ?
UNION ALL
SELECT tId, tUserId, tDate FROM tableC WHERE tUserId = ?
ORDER BY tDate DESC;

I honestly don't write many of these. 老实说,我没有写很多。 Maybe in large part because of ORMs (something to look into), or as the conversation has leaned towards, design. 可能在很大程度上是由于ORM(需要研究的东西),或者是因为对话倾向于设计。 Your first take with the left join is the format of probably 95% of the queries I write. left join的第一个选项是我编写的查询中可能有95%的格式。 Anyway though, there is no reason not to use a union for certain reporting needs. 无论如何,没有理由不为某些报告需求使用联合。 Maybe even create a view *gasp* . 甚至可以创建一个* gasp * view

first your SQL is not correct tableB dose not have the column tableB.tableAuserid 首先您的SQL不正确的tableB没有列tableB.tableAuserid

  SELECT tableA.*, tableB.*, tableC.*
  FROM tableA
  LEFT JOIN tableB
  ON tableA.tableAuserid = tableB.tableBuserid
  LEFT JOIN tableC
  ON tableA.tableAuserid = tableC.tableCuserid
  WHERE tableA.tableAuserid = 44
  ORDER BY tableAdate desc, tableBdate desc, tableCdate DESC

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

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