从数据库组合表中选择数据

时间:2014-03-04 00:09:48

标签: php mysql

数据库的结果

10000583221231 --> 10000583221233 --> 2014-02-10
10000583221231 --> 10000358040343 --> 2014-03-03
10000583221231 --> 10000583221546 --> 2014-03-21
10000583221231 --> 10000583221345 --> 2014-04-01

10000583221231 <-- 10000583221233 <-- 2014-02-10
10000583221946 <-- 10000583221233 <-- 2014-03-28
10000583221475 <-- 10000583221233 <-- 2014-04-01
10000583221564 <-- 10000583221233 <-- 2014-04-09
10000583221397 <-- 10000583221233 <-- 2014-04-20

php代码:

    <?php
include('db.php');
$result = mysqli_query($con,"SELECT * FROM Aplicatie WHERE id_usr_1 = '10000583221231' ORDER BY date");

while($row = mysqli_fetch_array($result))
  {
  echo $row['id_usr_1'] . " --> " . $row['id_usr_2'] . " --> " . $row['date'];
  echo "<br>";
  }

  echo '<br>';

$result_1 = mysqli_query($con,"SELECT * FROM Aplicatie WHERE id_usr_2 = '10000583221233' ORDER BY date");

while($row = mysqli_fetch_array($result_1))
  {
  echo $row['id_usr_1'] . " <-- " . $row['id_usr_2'] . " <-- " . $row['date'];
  echo "<br>";
  }
?>

我需要按照以下方式将它们组合成一个表:

10000583221231 --> 10000583221233 --> 2014-02-10
10000583221231 <-- 10000583221233 <-- 2014-02-10
10000583221231 --> 10000358040343 --> 2014-03-03
10000583221231 --> 10000583221546 --> 2014-03-21
10000583221946 <-- 10000583221233 <-- 2014-03-28
10000583221231 --> 10000583221345 --> 2014-04-01
10000583221475 <-- 10000583221233 <-- 2014-04-01
10000583221564 <-- 10000583221233 <-- 2014-04-09
10000583221397 <-- 10000583221233 <-- 2014-04-20

并且必须按日期排序。

我该怎么办?谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

您可以使用OR

中的WHERE进行一次查询
<?php
include('db.php');
$result = mysqli_query($con,"SELECT * FROM Aplicatie WHERE id_usr_1 = '10000583221231' OR id_usr_2 = '10000583221233' ORDER BY date");

while($row = mysqli_fetch_array($result))
  {

  $arrows = ($row['id_usr_1'] == '10000583221231') ? " --> " : " <-- ";

  echo $row['id_usr_1'] . $arrows . $row['id_usr_2'] . $arrows . $row['date'];
  echo "<br>";
  }

?>