PHP,SQL-获取表id=用户id的表,并计算行id=用户id的其他表

PHP,SQL-获取表id=用户id的表,并计算行id=用户id的其他表,php,mysql,sql,Php,Mysql,Sql,感谢您的帮助,首先我将显示代码: $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, $pocetZazn

感谢您的帮助,首先我将显示代码:

$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 ";
我需要获得用户列表customers表,该表按合同数contracts表排序

我试图通过在那里搜索来解决这个问题,但我不能。。。如果你能帮我解释一下它是如何工作的,谢谢你$pocetZanamu是记录的数量


我需要得到用户名,姓氏等。。。从表customers,按contracts表中的合同数排序,其中是contract_id,customer_contract user 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_合同,该表中有多条记录,在customer_合同列中包含相同的值。 如果只需要具有相同用户id的记录总数,则应使用:

$dotaz = "Select 1 FROM `contracts`   WHERE `user_id`=$id";
$results = $mysqli->query($dotaz);
$count = mysql_num_rows($results);

看起来您缺少一组单引号。此解决方案仅显示3个用户中的1个用户,但不显示列表,是的,请在,但是它只显示了1条记录它显示了未知列“contracts.customer\u contract”在“field list”中我试图将contracts.customer\u contract获取到“”,我检查了列的名称,结果是正确的,我假设您将文件名存储在contracts.customer\u contract中,因为这是您在表达式中使用的。我将使用的语法是将`SQL\u FieldName`计数为CNT,`SQL\u FieldName`来自。。。其中,SQL_FieldName是您试图计数的字段的名称。'SQL_FieldName'将同时且仅显示计数字段的计数和值。现在,我只在您查询合同的位置执行此操作。我相信查询字符串中的$_会话['user_id']会引发错误。这就是为什么我添加了$id=$\u会话['user\u id'],但不确定您到底想做什么。如果需要使用客户名称代替用户id,则需要加入客户表。这将得到看起来像CNT |客户|合同|用户| id的行