2015-07-20 105 views
1

嗨Stackoverflow的成員!從table1中選擇3個值並使用table2中table1中的一個值來獲取結果?

我得到了這個兩難問題:

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

我想獲得的所有flirted_id的由flirter_id「1」和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的人員的名字,中間名和姓氏。

  • 在此先感謝!

回答

0

如果我理解正確,你想做一個加盟抓住從表1,也是有關誰在表1

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; 

也被列爲added_id如果你的人在表2中的信息想了解有關adder_id的信息(假設人也存儲在表2):

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

+0

我不想要任何關於adder_id的信息,但謝謝反正! 那我該如何迴應結果呢? 就像這樣: echo $ row [「first」],$ row [「middle」],$ row [「last」]; 或如下所示: echo $ row [「t2.first」],$ row [「t2.middle」],$ row [「last」]; ? – bilbodog

+0

所以你想使用我的答案的第一個查詢:然後你可以使用'echo $ row ['first']'作爲列名,所以數組鍵是唯一的。 – bish

0

試試吧

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 
+0

而且我什麼都不懂!:O 我不喜歡這個MySQL的專業人員。我正在努力學習它。 – bilbodog

+0

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 –

+0

從這個sql中你可以得到added_id的名字 –

0

我發現我自己。我扼殺了我的大腦,並得到這個作爲答案:

       <?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(); 
           ?> 
相關問題