2016-12-07 48 views
-3
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

請格式化您的代碼。每行縮進4個或更多空格。 –

+0

通過使用下面的查詢我得到我需要的輸出 – Gowrisankar

+0

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

回答

0

btw,這是奇怪的int(2)有'150'。

'用戶' 也是在SQL的保留字,所以不要用它PLS

SELECT u.name,u.class, 
(SELECT SUM(subject) FROM marklist WHERE subject LIKE 'biology') AS biology, 
(SELECT SUM(subject) FROM marklist WHERE subject LIKE 'chemistry') AS chemistry,m.outof AS outoftotal,m.markscored AS  
totalmarkscored,m.markscored/m.outof*100 
FROM user u, marklist m 
WHERE u.id=m.id 
+0

'(2)'只是可選的顯示寬度,可以存儲全部的'int'數據類型參考:[數字類型屬性](http://dev.mysql.com/doc/refman/5.5/en/numeric -type-attributes.html) – bansi

+0

哇,我不知道這件事。很好知道@bansi但仍然不是它應該是int(3)? – MVCNoob

+0

先生,我想要一個輸出作爲字段列表: - 名稱,類,標記,totalmarkscored,總的outof,並消除行主題和添加行像生物學,化學,物理等,請給我一個查詢插入生物學,化學,物理行 – Gowrisankar

0

感謝您MVCNoob,執行修改後的查詢後: 選擇u.id,u.name,u.class, (SELECT SUM(markscored)FROM marklist WHERE subject LIKE'chemistry'AND u.id = m.id) (SELECT SUM(markscored)FROM marklist WHERE subject LIKE'chemistry'AND u.id = m.id )AS化學, (SELECT SUM(markscored)FROM marklist WHERE受試者LIKE '物理' AND u.id = m.id)AS物理, (SELECT SUM(markscored)FROM marklist WHERE u.id = m.id)AS總, (SELECT SUM(outof)FROM marklist WHERE u.id = m.id)AS outof, ((SELECT SUM(markscored)FROM marklist WHERE u.id = m.id)/(SELECT SUM(outof)FROM marklist WHERE u.id = m.id)* 100)as p FROM u,marklist m WHERE u.id = m.userid = 1; ,我得到一個輸出爲: + ------ + ------ + ------- + --------- + --------- - + --------- + ------- + ------- + --------- + | id |名稱| class |生物學|化學|物理|總| | outof | p | + ------ + ------ + ------- + --------- + ----------- + ---- ----- + ------- + ------- + --------- + | 1 | Arun | 6 | 37 | 48 | 48 | 133 | 150 | 88.6667 | | 1 | Arun | 6 | NULL | NULL | NULL | NULL | NULL | NULL | | 1 | Arun | 6 | NULL | NULL | NULL | NULL | NULL | NULL | + ------ + ------ + ------- + --------- + ----------- + ---- ----- + ------- + ------- + --------- + 設置3行(0.00秒),這裏阿倫在重複請親切地讓我查詢以避免重複 感謝您