describe user;
id int(1) NO 'PRI' 0
name 'varchar'(20) YES NULL
address 'varchar'(20) YES NULL
class int(2) YES NULL
DOB date YES NULL
created date YES NULL
describe 'marklist'
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| id | int(1) | NO | | 0 | |
| subject | varchar(10) | YES | | NULL | |
| 'markscored'| int(2) | YES | | NULL | |
| 'outof' | int(2) | YES | | NULL | |
| 'userid' | int(2) | YES |'NUL'| NULL | |
+-------------+-------------+------+-----+---------+-------+
這些都是給出的兩個表,並根據這兩個我需要出去放是因爲:加入兩個表產生給定輸出
|name |class|biology|chemistry|physics|outoftotal|totalmarkscored|percentage|
+-----+-----+-------+---------+-------+----------+---------------+----------+
|arun | 5 | 45 | 42 | 40 | 150 | 127 |84.6 |
請給我一個查詢來獲取上述輸出使用這兩個表用戶和標記'
請格式化您的代碼。每行縮進4個或更多空格。 –
通過使用下面的查詢我得到我需要的輸出 – Gowrisankar
SELECT u.name,u.class, SUM(CASE WHEN subject subject like'biology'THEN markscored ELSE 0 END)AS biology, SUM(CASE WHEN subject LIKE'chemistry' THEN ELSE markscored 0 END)AS化學, SUM(CASE WHEN受試者LIKE '物理' THEN ELSE 0 END markscored)AS物理, SUM(markscored)AS TOTAL, SUM(outof)AS oUTOF 從u,marklist米 WHERE u.id = m.userid GROUP BY u.name,u.class; – Gowrisankar