[英]CodeIgniter count within query as extra column
I am having a query in CodeIgniter which is working fine. 我在CodeIgniter中有一个查询,工作正常。 Now I would like to have an additional COUNT inside per output line, this data is shown inside a table. 现在,我想在每个输出行中包含一个额外的COUNT,该数据显示在表中。 I want to see per line how much times the user has requested a device. 我想每行查看用户请求设备的次数。
I don't want the output to be grouped, I want one column per row that says this user has 3 requests, so in this case 3 rows from the same user, having a different deviceRequest, with 3 times in this column count being 3. 我不希望将输出分组,我希望每行一列表示该用户有3个请求,因此在这种情况下,来自同一用户的3行具有不同的deviceRequest,此列中的3次为3 。
PS: I am quite new to this, info about your solution would be appreciated. PS:我对此很陌生,有关您的解决方案的信息将不胜感激。
Query: 查询:
$datatables->select('
deviceRequestID,
deviceRequestStatuses.deviceRequestStatusShortname,
users.userEmail,
users.userID,
users.userName,
users.userSurname,
deviceRequestComments,
deviceRequests.createdAt,
deviceRequests.updatedAt,
rooms.roomName,
hotspots.hotspotName,
hotspots.hotspotAddress,
hotspots.hotspotCity,
hotspots.hotspotZIP,
DATEDIFF(CURDATE(), deviceRequests.createdAt) AS daysDifference,
')
->join('users', 'deviceRequests.userID=users.userID', 'left')
->join('deviceRequestStatuses', 'deviceRequests.deviceRequestStatusID=deviceRequestStatuses.deviceRequestStatusID')
->join('rooms', 'users.roomID=rooms.roomID', 'left')
->join('hotspots', 'rooms.hotspotID=hotspots.hotspotID', 'left')
->from('deviceRequests');
我在上面查询的选择部分内添加了这条额外的查询行,它恰好满足了我的需要。
(SELECT count(userID) FROM deviceRequests WHERE userID = users.userID) AS deviceCount
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.