我正在显示来自数据库的数据,但是我希望显示一次相同的id但在其他列中显示它存储在MySQL数据库中的次数。 谢谢!
if (!isset($_REQUEST['completed_consu_id'])) {
$query = "SELECT * FROM completed_consumers";
} else {
$query = "SELECT * FROM completed_consumers WHERE consu_id=consu_id";
}
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$numberofrow=mysql_num_rows($result);
while ($row = @mysql_fetch_array($result)) {
$consu_id = $row['consu_id'];
$consu_first_name = $row['consu_first_name'];
$consu_last_name = $row['consu_last_name'];
$consu_phone = $row['consu_phone'];
$consu_email = $row['consu_email'];
$consu_address = $row['consu_address'];
$consu_city = $row['consu_city'];
$consu_state = $row['consu_state'];
$consu_zip = $row['consu_zip'];
$consu_IP = $row['consu_IP'];
$status = $row['status'];
$query2 ="SELECT consu_id, COUNT(*) FROM completed_consumers WHERE consu_id=$consu_id GROUP BY consu_id";
$result2 = mysql_query($query2) or die('Query failed: ' . mysql_error());
$numberofrow2=mysql_num_rows($result2);
$times= $numberofrow2;
echo $times;
这是我编写的代码,它显示了所有消费者的详细信息以及他们输入了多少数据或详细信息,但我想显示一次消费者详细信息/名称,但是他们在“时间”列中提交了多少次数据。
像大卫消费者增加了2次,Monika添加了1次,Arshi添加了3次数据/细节,但是当我检索数据时它在表中显示6行,我想显示3行,1表示David,1表示Monika ,1为Arshi,但是当我检索详细信息时,他们在“时间”列中添加了多少次。
答案 0 :(得分:1)
查看GROUP BY条款。
selectFromWhere = db('orders')
.select('orders.*', db.raw('IFNULL(??, 0) as ??', ['x.unread', 'unread_messages']))
.leftJoin(
db('chats')
.select('id_order', db.raw('count(*) as ??', ['unread']))
.where('read_by_user', 0)
.groupBy('id_order')
.as('x'),
'x.id_order',
'orders.id_order'
)
.where('id_customer', req.user.id_customer);