【问题标题】:PHP get value of row field from inside of while loopPHP从while循环内部获取行字段的值
【发布时间】:2023-03-05 06:21:01
【问题描述】:

假设我有这样一张桌子:

swipes

swp_id    swp_by    swp_to    swp_type    swp_date
1           8          1       top        2020-03-24 02:39:12
2           11         1       right      2020-03-18 02:37:58
3           1          8       right      2020-03-31 04:04:40
4           1         11       top        2020-03-31 04:04:40

基于我的设置如下:

<?php
  $stmt = $pdo->prepare("SELECT * FROM swipes WHERE swp_by = :mem AND swp_type != 'left' ORDER BY swp_id DESC");
  $stmt-> bindValue(':mem', $sessionUser);
  $stmt-> execute();

  while($swp = $stmt->fetch()){
      $match = $pdo->prepare("SELECT swp_id, swp_date FROM swipes WHERE swp_by = :by AND swp_to = :to AND swp_type != 'left'");
      $match-> bindValue(':by', $swp['swp_by']);
      $match-> bindValue(':to', $sessionUser);
      $match-> execute();

      while($mat = $match->fetch()){
?>
  <!-- Some HTML Part here to display fetched data as per second while loop -->
<?php } } ?>

正如您现在所看到的,我在第一个查询中有ORDER BY swp_id DESC,它工作正常。但是当我尝试从第一个查询中删除并在第二个查询中添加它时它不起作用。我真正想做的是从最近的滑动中获取时间。

正如您在上面给出的表格中看到的,对于带有swp_id 1 和 2 的第一两行,用户 8 和 11 首先刷了用户 1。后来,用户 1 刷了用户 8 和 11,正如您在 swp_id 3 和 4 的记录中看到的那样。现在,记录 3 和 4 是最新记录。因此,我想从这些记录而不是从 1 和 2 中获取 swp_date。现在,它正在从记录 1 和 2 中返回日期。

【问题讨论】:

  • 如果您向我们展示这两个相关表格中的数据,可能有助于我们了解您的情况
  • @RiggsFolly 此处的其他表格不是强制性的。我会更新问题。
  • @RiggsFolly 我已经更新了这个问题。仅涉及 1 个表。我之前没有费心去清理它。但是这些表对这个问题并不重要。
  • 我认为您混淆了 swipe_byswipe_to 值。 ORDER 仅对您检索的数据进行排序,与您选择的内容无关。
  • @Michel 不,这些值没问题。简而言之,我在这里真正想要的是从记录 3 和 4 而不是 1 和 2 中获取 swp_date 值,因为记录 3 和 4 是 1 和 2 的匹配记录并出现在第二位。

标签: php mysql sql pdo


【解决方案1】:

假设您想要滑动到 user_id 1 的用户的所有 user_id 和日期,但前提是他(她)也滑动了该用户,而不是向左滑动。

所以稍微扩展一下你的桌子:

swp_by  swp_to  swp_type    swp_date
   8      1       top         ...
   8      4       top         ... <not
   11     1       top         ...
   1      8       top         ...
   1      11      top         ...
   20     1       top         ... < not
   1      40      top         ... < not
   1      41      left        ... < not

第一部分:从 user 1 swp_by = 1 中选择所有未留下的滑动(与您的第一个查询相同):

// :mem = 1
SELECT swp_id, swp_by, swp_to, swp_date 
    FROM swipes
    WHERE swp_by = :mem AND swp_type <> "left"

结果 1:

swp_by  swp_to  swp_type    swp_date
   1      8        top         ...
   1     11        top         ...
   1     40        top         ... <not

第二部分:找到所有“返回”到用户1的滑动,所以swp_to = 1

// :mem = 1
SELECT swp_id, swp_by, swp_to, swp_date 
    FROM swipes
    WHERE swp_to = :mem AND swp_type <> "left"

结果 2:

swp_by  swp_to  swp_type    swp_date
   8      1       top         ...
   11     1       top         ...
   20     1       top         ... < not

现在您可以交叉引用从 Result 1 (8, 11, 40) 中找到 swp_to 数字的结果,这些数字在 Result 2 ( 8, 11, 20)。这可以使用同一张表中的LEFT JOIN 来完成

//this, in combination with WHERE, gives Result 1
SELECT R1.swp_id, R1.swp_to, R1.swp_type , GREATEST(R1.swp_date,R2.swp_date)

    FROM swipes AS R1

//cross reference with Result 2
LEFT JOIN swipes AS R2
    ON ( 
    //look for those records where R1.swipe_to = R2.swipe_by
    R1.swp_to = R2.swp_by 
        AND
    //swipe_to from Result 2 has to be user 1
    R2.swp_to = R1.swp_by
        AND 
    //no left swipes in Result 2
    R2.swp_type <> "left" 
    )

WHERE 
    //look only at the records from user 1...
    R1.swp_by = :mem 
        AND
    //...that are not empty
    R2.swp_by IS NOT NULL
            AND
    //... that not are left
    R1.swp_type <> 'left' 
ORDER BY R1.swp_id DESC

这是new fiddle *EDIT OP 想要最新日期,用GREATEST 调整日期

【讨论】:

  • 我看到了你的小提琴。你几乎把一切都做好了。唯一缺少的是它仍然没有根据最新记录给出结果。在你的小提琴中,如果我们记录 11 和 12,我们可以看到 1 在记录 11 处先刷了 10,因此,它不是最新记录。应该已经为记录 12 显示日期,其中用户 10 稍后刷了 1 使其成为最新记录。显示的日期应该是 1 月 12 日而不是 1 月 11 日。那里的日期表示用户 1 和 10 实际匹配的日期。当用户 1 在 1 月 11 日刷了 10 次时,他们不匹配....(下一条评论)
  • 1 月 12 日,当 10 将用户 1 刷回来时,他们成为了一场比赛。请看,你快到了,这就是我遇到的实际问题。
  • 调整了查询​​和小提琴,他们现在给你想要的。但在未来的情况下,请更好地解释你想要什么。
  • 修复了小提琴
  • 谢谢.. 现在我有我想要的了。感谢这么多的努力。我会永远感激不尽:)
【解决方案2】:

如果从SessionUser=8开始,第一个循环恢复swp_by=8,swp_id=1。然后内循环要求 swp_by=8 和 swp_to=8,即没有结果。您只能看到第一个循环的结果。我想你必须检查你的第二个查询。也许:

$match-> bindValue(':by', $swp['swp_to']);
$match-> bindValue(':to', $swp['swp_by']);

【讨论】:

  • 在第二个循环中可以修改选择:while($swp = $stmt->fetch()){ $match = $pdo->prepare("SELECT swp_id, swp_date FROM swipes WHERE swp_to = :to AND swp_type != 'left'"); $match-> bindValue(':to', $sessionUser); $match->execute();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-06
  • 1970-01-01
  • 2021-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多