如何使子查詢和總與個人用戶的印記?目前,我只能得到第一個用戶的標誌,我怎麼能得到每個用戶的標誌? 這裏是小提琴http://sqlfiddle.com/#!9/d6d68/3
數據集...
SELECT * FROM positions;
+----+-----------+---------------+------------+---------------+---------------------+---------------------+
| id | code_name | display_name | category | default_merit | created_at | updated_at |
+----+-----------+---------------+------------+---------------+---------------------+---------------------+
| 1 | K01 | Chairman | College | 8 | 2016-06-08 10:30:54 | 2016-06-08 10:30:54 |
| 2 | K02 | Vice Chairman | College | 6 | 2016-06-08 10:30:54 | 2016-06-08 10:30:54 |
| 3 | K03 | Treasurer | College | 6 | 2016-06-08 10:30:54 | 2016-06-08 10:30:54 |
| 4 | K04 | Secretary | College | 6 | 2016-06-08 10:30:54 | 2016-06-08 10:30:54 |
| 5 | K05 | Committee | College | 4 | 2016-06-08 10:30:54 | 2016-06-08 10:30:54 |
| 6 | K06 | Participator | College | 1 | 2016-06-08 10:30:54 | 2016-06-08 10:30:54 |
| 7 | U01 | Chairman | University | 10 | 2016-06-08 10:30:54 | 2016-06-08 10:30:54 |
| 8 | U02 | Vice Chairman | University | 8 | 2016-06-08 10:30:54 | 2016-06-08 10:30:54 |
| 9 | U03 | Treasurer | University | 8 | 2016-06-08 10:30:54 | 2016-06-08 10:30:54 |
| 10 | U04 | Secretary | University | 8 | 2016-06-08 10:30:54 | 2016-06-08 10:30:54 |
| 11 | U05 | Committee | University | 5 | 2016-06-08 10:30:54 | 2016-06-08 10:30:54 |
| 12 | U06 | Participator | University | 2 | 2016-06-08 10:30:55 | 2016-06-08 10:30:55 |
+----+-----------+---------------+------------+---------------+---------------------+---------------------+
SELECT * FROM records;
+----+---------+-------------+--------+---------+---------------+-----------------+------------+------------+------------+---------------------+---------------------+
| id | user_id | position_id | finish | approve | recordable_id | recordable_type | deleted_at | created_by | approve_by | created_at | updated_at |
+----+---------+-------------+--------+---------+---------------+-----------------+------------+------------+------------+---------------------+---------------------+
| 1 | 1 | 1 | 1 | 1 | 1 | App\Event | NULL | 1 | 1 | 2016-06-08 10:30:55 | 2016-06-08 10:30:55 |
| 2 | 1 | 2 | 1 | 0 | 2 | App\Event | NULL | 1 | 1 | 2016-06-08 10:30:55 | 2016-06-08 10:30:55 |
| 3 | 2 | 2 | 1 | 0 | 2 | App\Event | NULL | 1 | 1 | 2016-06-08 10:30:55 | 2016-06-08 10:30:55 |
| 4 | 2 | 2 | 1 | 0 | 2 | App\Club | NULL | 1 | 1 | 2016-06-08 10:30:55 | 2016-06-08 10:30:55 |
| 5 | 2 | 2 | 1 | 0 | 1 | App\Sub_Event | NULL | 1 | 1 | 2016-06-08 10:30:55 | 2016-06-08 10:30:55 |
| 6 | 3 | 2 | 1 | 0 | 2 | App\Club | NULL | 1 | 1 | 2016-06-08 10:30:56 | 2016-06-08 10:30:56 |
| 7 | 2 | 3 | 1 | 0 | 2 | App\Club | NULL | 1 | 1 | 2016-06-08 10:30:56 | 2016-06-08 10:30:56 |
+----+---------+-------------+--------+---------+---------------+-----------------+------------+------------+------------+---------------------+---------------------+
SELECT * FROM users;
+----+----------------+--------+-------------------------+----------+----------------+------------+---------------------+---------------------+-----------+----------+--------+-----------+-------------+-----------+------------+
| id | name | matrix | email | password | remember_token | deleted_at | created_at | updated_at | attention | resident | banned | meritbook | must_inlist | protected | last_login |
+----+----------------+--------+-------------------------+----------+----------------+------------+---------------------+---------------------+-----------+----------+--------+-----------+-------------+-----------+------------+
| 1 | Lim Yoong Kang | 47408 | [email protected] | Szha6... | NULL | NULL | 2016-06-08 10:30:53 | 2016-06-08 10:31:05 | 0 | 1 | 0 | 0 | 0 | 0 | NULL |
| 2 | Admin John | 00001 | [email protected] | HYSPq... | NULL | NULL | 2016-06-08 10:30:53 | 2016-06-08 10:30:53 | 1 | 1 | 0 | 0 | 0 | 0 | NULL |
| 3 | Moderator Wong | 00002 | [email protected] | fvxWS... | NULL | NULL | 2016-06-08 10:30:54 | 2016-06-08 10:30:54 | 1 | 1 | 0 | 0 | 0 | 0 | NULL |
| 4 | Student Tai | 00003 | taileepeng[email protected] | ihdF6... | NULL | NULL | 2016-06-08 10:30:54 | 2016-06-08 10:30:54 | 1 | 1 | 0 | 0 | 0 | 0 | NULL |
+----+----------------+--------+-------------------------+----------+----------------+------------+---------------------+---------------------+-----------+----------+--------+-----------+-------------+-----------+------------+
和期望的結果
+--------+----------------+-------+
| matrix | name | total |
+--------+----------------+-------+
| 1 | Admin John | 0 |
| 2 | Moderator Wong | 0 |
| 3 | Student Tai | 0 |
| 4 | Lim Yoong Kang | 8 |
+--------+----------------+-------+
,我目前使用
SELECT
`users`.`matrix`,
`users`.`name`,
(SELECT SUM(positions.default_merit)
FROM `users`
INNER JOIN `records` ON `records`.`user_id` = `users`.`id`
INNER JOIN `positions` ON `positions`.`id` = `records`.`position_id`
WHERE `records`.`finish` = '1' AND `records`.`approve` = '1' AND `users`.`deleted_at` IS NULL) AS total
FROM `users`
GROUP BY `users`.`matrix`
HAVING total >= 0
LIMIT 100
SQL查詢
請注意,沒有ORDER BY的LIMIT是相當無意義的 - 即使使用了GROUP BY子句。這本手冊詳細討論了這個問題(但不是很清楚)。 – Strawberry
@Strawberry感謝您的建議。我將來會使用Limit with OrderBy。我太新手了,非常感謝你的幫助。 –
位置表中沒有user_id。同樣,在記錄表中的display_name。 – Strawberry