【问题标题】:Select 3 values from table1 and use one of the values from table1 in table2 to fetch results?从 table1 中选择 3 个值并使用 table2 中的 table1 中的一个值来获取结果?
【发布时间】:2015-07-20 04:51:42
【问题描述】:

您好 Stackoverflow 成员!

我遇到了这个难题:

users  ID  username  firstname  middlename lastname
        1  bilbodog    Casper               Thomsen
        2  bilbodog2   Smith      John      Andersen


flirts    flirter_id  flirted_id  date  time
              1          2          X    X

我想获取由 flirter_id '1' 生成的所有 flirted_id,并将 flirted_id 与用户进行比较,并从 flirted_id 中收集名字、中间名和姓氏。另外,我想从调情表中收集日期和时间。然后我想在 HTML 表格中回显结果。

所以现在我正在这样做:

<?php
$ID=$_SESSION['ID'];
// Create connection
$conn = new mysqli($mysql_hostname, $mysql_user, $mysql_password, $mysql_database);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT flirted_id, date, time FROM flirts WHERE flirter_id='$ID'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
  ----> ECHO TABLE <----
} else {
  echo 'No flirts found.';
}
$conn->close();
?>

但这不起作用。

我被困在这一点上。我不知道如何从 1 个表中收集 3 个结果并使用其中一个结果在表 2 中进行比较,以收集按该 ID 的人的名字、中间名和姓氏。

  • 提前致谢!

【问题讨论】:

    标签: php mysql sql-server


    【解决方案1】:

    如果我对您的理解正确,您想要进行联接以获取 table1 中的信息以及 table1 中列为 added_id 的人员的信息

    select t1.date, t1.time, t2.first, t2.middle, t2.last
    from table1 t1
    join table2 t2 on (t1.added_id = t2.id)
    where t1.adder_id = 1;
    

    如果您还想要adder_id 的信息(假设人员也存储在 table2 中):

    select t1.date, t1.time, t2.first, t2.middle. t2.last, t3.first, t3.middle, t3.last
    from table1 t1
    join table2 t2 on (t1.added_id = t2.id)
    join table2 t3 on (t1.added_id = t3.id)
    where t1.adder_id = 1;    
    

    在这种情况下,您应该给人员 cols 别名以更清楚地识别他们,例如t2.first as added_id_first, t3.first as adder_id_first等等

    注意我假设adder_idadded_id 字段中没有NULL 值。如果他们可以在语句中使用LEFT JOIN 而不是JOIN

    【讨论】:

    • 我不想要关于 adder_id 的任何信息,但无论如何谢谢!那么我应该如何回应结果呢?就像这样: echo $row["first"], $row["middle"], $row["last"];或像这样: echo $row["t2.first"], $row["t2.middle"], $row["last"]; ?
    • 所以你想使用我回答的第一个查询:然后你可以使用 echo $row['first'] 作为列名,所以数组键是唯一的。
    【解决方案2】:

    试试看

    select concat (table2.first," ",table2.middle ) as adder_id_Name,AddedName.* from table2,(SELECT concat (tb2.first," ",tb2.middle ) as added_id_Name,  tb1.adder_id from table2 tb2,table1 tb1 where tb1.added_id = tb2.ID) as AddedName where table2.ID = AddedName.adder_id
    

    【讨论】:

    • 而且我什么都不懂! :O 我还不是所有这些 MySQL 的专业人士。我正在努力学习它。
    • First ---> SELECT concat (tb2.first," ",tb2.middle ) as added_id_Name, tb1.adder_id from table2 tb2,table1 tb1 where tb1. added_id = tb2.ID
    • 从这个 sql 你得到 added_id 的名字
    • @bilbodog concat() 正在将字符串组合成一个字符串。因此,将名字和中间名组合成一个字符串,并在它们之间放置一个空格。之后将这个新的虚构 col 命名为 added_id_Name
    • 就像我想的那样,那些完全无法理解的高级解释。我的页面刚刚出现错误。“语法错误,意外 '",tb2.middlename ) as added_id' (T_CONSTANT_ENCAPSED_STRING)"
    【解决方案3】:

    我自己发现的。我绞尽脑汁得到了这个答案:

                                  <?php
                                        // Create connection
                                        $conn = new mysqli($mysql_hostname, $mysql_user, $mysql_password, $mysql_database);
                                        // Check connection
                                        if ($conn->connect_error) {
                                            die("Connection failed: " . $conn->connect_error);
                                        }
                                            $sql = "SELECT from_id, message, date, time FROM messages WHERE to_id='$ID'";
                                            $result = $conn->query($sql);
                                        if ($result->num_rows > 0) {
                                            // output data of each row
                                            while($row = $result->fetch_assoc()) {
                                                $from_id=$row["from_id"];
                                                $message=$row["message"];
                                                $date=$row["date"];
                                                $time=$row["time"];
                                                $sql10 = "SELECT firstname, middlename, lastname FROM users WHERE ID='$from_id'"; 
                                                $sql11 = mysql_query($sql10); 
                                                $sql12 = mysql_fetch_row($sql11);
                                                $firstname=$sql12[0];
                                                $middlename=$sql12[1];
                                                $lastname=$sql12[2];
                                                ?>
                                                <a href="#" class="list-group-item">
                                                    <span class="label label-default"><?php echo $date, ' ', $time; ?></span>
                                                    <span class="fa fa-star"></span>
                                                    <span class="name" style="min-width: 120px; display: inline-block;">Casper Thomsen</span>
                                                    <span class="text-muted" style="font-size: 11px;">- <?php echo $message; ?></span>
                                                    <span class="pull-right">
                                                        <button type="submit" id="view_message" name="view_message" class="btn btn-default btn-xs pull-right">L&aelig;s  Besked <i class="fa fa-angle-right"></i></button>
                                                    </span>
                                                </a>
                                                <?php
                                            }
                                        } else {
                                            echo '
                                            <div class="block block-team_member_block span12 first cf">
                                                <div class="member">
                                                    <div class="member-social">
                                                    </div>
                                                    <h3>No members or could not connect to database..</h3>
                                                    <div class="bline"></div>
                                                </div>
                                            </div>';
                                        }
                                        $conn->close();
                                    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-28
      • 1970-01-01
      • 2016-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多