简体   繁体   中英

CodeIgniter count within query as extra column

I am having a query in CodeIgniter which is working fine. Now I would like to have an additional COUNT inside per output line, this data is shown inside a table. 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.

PS: I am quite new to this, info about your solution would be appreciated.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM