如何比较来自同一数据库的两个表中的值

时间:2018-03-27 20:34:41

标签: php mysql

我一直在尝试将表中的值与另一个表中的另一个值进行比较,但只有else部分正在执行。

<?php 

  $sql = "SELECT * FROM user WHERE user_id=$row[user_id]";
  $result = $conn -> query ($sql);
  if ($result -> num_rows > 0) {
  while ($row = $result ->fetch_assoc()) {
  $sql1 = "SELECT * FROM jobpost WHERE jobpost_id=$_GET[id] ";

  $result1 = $conn -> query ($sql1);
  $row_count = mysqli_num_rows($result);
  $row1_count = mysqli_num_rows($result1);
  $remaining_rows = min($row_count, $row1_count);
  $row = mysqli_fetch_assoc($result);
  $row1 = mysqli_fetch_assoc($result1);
  if($row["experience"] > $row1["experience"])
  {
    //some code to display something
    echo "1"; 
  }
   else
        {
         echo "2";

         }

    } } 
    ?>

1 个答案:

答案 0 :(得分:0)

欢迎来到SO,很难阅读您的源代码。您可以使用这样的简单查询:

SELECT IF(u.experience > j.experience,1,0) FROM 
(
  (SELECT experience FROM user WHERE user_id = 1) u
   JOIN
  (SELECT experience FROM jobpost WHERE jobpost_id = 8) j
)

在此处尝试此查询:http://sqlfiddle.com/#!9/1742c0

请检查我们的数据源。也许其他情况是正确的。