[英]PHP, SQL - getting fetch where table id = user id and count other table where row is = user id
感謝您的幫助,首先我將顯示代碼:
$dotaz = "Select * from customers JOIN contracts where customers.user_id ='".$_SESSION['user_id']."' and contracts.customer_contract = ".$_SESSION['user_id']." order by COUNT(contracts.customer_contract) DESC limit $limit, $pocetZaznamu ";
我需要獲取按contracts(contracts table)
計數contracts(contracts table)
排序的用戶列表(客戶contracts(contracts table)
我試圖通過在那邊搜索來解決此問題,但我不能...如果您能幫助我並解釋它的工作原理,謝謝! :) $pocetZanamu
是記錄數。
我需要從表客戶那里獲取用戶(姓名,姓氏等),並按合同表中的合同數量排序,其中contract_id,customer_contract(用戶ID)是。
這應該在您要計數的列名稱所在的位置執行。
$id = $_SESSION['user_id'] ;
$dotaz = "Select COUNT(`customer_contract`) AS CNT, `customer_contract` FROM `contracts` WHERE `user_id`=$id GROUP BY `customer_contract` ORDER BY `CNT` DESC";
根據您的操作,您可能希望將結果存儲在數組中,然后分別處理數組中的每個元素。
while ($row = mysqli_fetch_array($results, MYSQL_NUM)){
$contracts[$row[1]] = $row[0];
}
foreach ($contracts AS $customer_contract => $count){
Process each user id code here
}
不知道您在計數什么。 上面為一個表計算了customer_contract,該表的多個記錄在customer_contract列中包含相同的值。
如果您只想要具有相同user_id的記錄總數,則可以使用:
$dotaz = "Select 1 FROM `contracts` WHERE `user_id`=$id";
$results = $mysqli->query($dotaz);
$count = mysql_num_rows($results);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.