2016-08-28 161 views
0

963排在用戶表中的視頻表
872行多選擇查詢

在用戶表(FBID,名)列名
在視頻表(FBID,等..)是科拉姆名。

使用下面的查詢,我通過從視頻表提供fbid從用戶表獲取名稱。下面的查詢只返回791行,​​但它應該返回872。

<?php 
$counter = 1; 
$q = "SELECT * FROM videos GROUP BY fbid ORDER BY score DESC, id ASC"; 
$r = mysqli_query($conn,$q); 
if(mysqli_num_rows($r)>0): 
    while($row = mysqli_fetch_assoc($r)): 
     $fbid=$row['fbid']; 

     $q1 = "SELECT name FROM users WHERE fbid=".$fbid.""; 
     $r1 = mysqli_query($conn,$q1); 
     while($row1 = mysqli_fetch_assoc($r1)): 
      $name=$row1['name']; 
      ?> 

      <?php 
      $counter++; 
     endwhile; 
    endwhile; 
endif; 
?> 
+0

其Facebook的ID,它應該是相同的。跨2個表 – Zack

+0

我不熟悉冒號語法。它是否比花括號提供了一些優勢? – Strawberry

+1

一個好的經驗法則是'從不在循環內執行查詢'。然而,在這種情況下,你甚至不需要循環 - 一個連接就足夠了。 – Strawberry

回答

0

表控股重複81(872-791)fbid。

替換此查詢

$q = "SELECT * FROM videos GROUP BY fbid ORDER BY score DESC, id ASC"; 

有:如果你去791個結果

$q = "SELECT * FROM videos ORDER BY score DESC, id ASC"; 

你會得到872

+0

我想SELECT * FROM視頻GROUP BY FBID,沒有重複 – Zack

+0

SELECT COUNT(*)FROM視頻ORDER BY DESC分,ID ASC 請執行這個查詢,並檢查總戰績 –

+0

872,沒有重複。 – Zack

0

我修改了代碼的查詢

<?php 

$counter = 0; 
$q = "SELECT DISTINCT * FROM `videos` ORDER BY `fbid` ASC"; 
$r = mysqli_query($conn,$q); 
if(mysqli_num_rows($r)>0): 
while($row = mysqli_fetch_assoc($r)): 
$fbid=$row['fbid']; 

$q1 = "SELECT * FROM `user` WHERE `fbid`= $fbid "; 
$r1 = mysqli_query($conn,$q1); 
while($row1 = mysqli_fetch_assoc($r1)): 
$name=$row1['nombre']; 
echo $name . "<br/>; 
?> 

<?php 
$counter++; 
endwhile; 
endwhile; 
endif; 

echo "Total " .$counter; 
?> 

可能是因爲s青梅視頻FBID表不在表檢查有多少用戶此查詢

SELECT count(fbid) FROM `videos` where fbid NOT IN(SELECT fbid FROM `user`); 
1
SELECT v.* 
    , u.name 
    FROM videos v 
    JOIN users u 
    ON u.fbid = v.fbid 
    ORDER 
    BY v.score DESC 
    , v.id ASC;