求一sql查詢語句,題目如圖,求一個SQL查詢語句寫法,有點複雜 高手進

2022-09-13 18:40:20 字數 4381 閱讀 8510

1樓:匿名使用者

需要5個子查詢,然後用left join

select distinct u.username as 使用者名稱, t1.密碼 as 密碼,t2.日期 as 日期,t3.分組 as 分組。。。

from users u

left join

(select distinct username as 使用者名稱, val as 密碼

from users where attrtype='ietf|2'

)t1 on u.使用者名稱 = t1.使用者名稱

left join

(select distinct username as 使用者名稱, val as 日期

from users where attrtype='tekradius|2'

)t2 on u.使用者名稱 = t2.使用者名稱

如果資料量比較大(比如百萬級),這種資料庫設計很不合理。

如果資料只有幾萬條,sql server, oracle 都沒有問題。

2樓:

select 使用者名稱=username,密碼=max(case when attribute='ietf|0' then val end),

日期=max(case when attribute='ietf|1' then val end),

分組=max(case when attribute='ietf|2' then val end)

from users t1

group by username

不改資料庫結構可以用case 判斷attrtype的值,每種值取對應的val,如上所示。

3樓:孤傲靈魂→玄燁

查詢相同的使用者用 select * from 表名 order by username

查詢 指定欄位的資訊 select 你要的欄位名 from 表名

需要排序加 order by

4樓:匿名使用者

用子查詢 應該可以做到

這種重複的資料在資料庫中存在 本身就說明了一個問題 你的資料庫結構不合理

設計資料庫的幾個原理應該要理解下 資料的冗餘是對資料庫效率有著很大的影響,這種資料一多

你的資料庫的負荷也就越高,效率也就越慢,最好重新設計下

5樓:匿名使用者

select * from users where username like '%%'

這個語句就可以查出

求一個sql查詢語句

sql查詢簡單語句一個疑問求解,如圖這是三張表

求一條sql查詢語句,急!! 20

【急】如圖題目:請寫出以下操作的sql語句。(1~10題)

6樓:匿名使用者

……你表結構至少也要貼出來啊

求一個sql語句,很簡單的,麻煩大家了..

7樓:

需求跟這個一模一樣,自已照著做就可以了

8樓:

select 姓名,

sum(case 科目 when '語文' then 成績 else 0 end) 語文,

sum(case 科目 when '數學' then 成績 else 0 end) 數學,

sum(case 科目 when '英語' then 成績 else 0 end) 英語,

sum(case 科目 when '化學' then 成績 else 0 end) 化學

from 表

group by 姓名

9樓:匿名使用者

呵呵,這個問題其實並不是像樓主想像的那麼簡單,這是一個交叉查詢的問題:

1、準備資料在資料庫mydatabase中建立表scores

use mydatabase

if exists(select id from sysobjects where name='scores')

drop table scores--刪除與實驗衝突的表

gocreate table scores (id int identity(1,1),student_name nvarchar(50),department nvarchar(50),scores int)

insert into scores select '張三','語文',80

union select '李四','語文',85

union select '張三','數學',95

union select '李四','數學',82

union select '張三','英語',84

union select '李四','英語',92

2、我們先利用case when then else end 語句將行轉為列:

select 學生姓名=student_name,語文=case when department='語文' then scores else 0 end from scores

這裡為了便於理解,只取一列,執行命令後返回結果如下:

學生姓名 語文

李四 0

李四 0

李四 85

張三 0

張三 0

張三 80

將其它兩列也新增進來:

select student_name as '學生姓名',

case department when '語文' then scores else 0 end

as '語文',

case department when '數學' then scores else 0 end

as '數學',

case department when '英語' then scores else 0 end

as '英語'

from scores

查詢後返回結果如下:

學生姓名 語文 數學 英語

李四 0 82 0

李四 0 0 92

李四 85 0 0

張三 0 95 0

張三 0 0 84

張三 80 0 0

現在只要把student_name相同的行合併到一起就完成了,

select student_name as '學生姓名',

sum(case department when '語文' then scores else 0 end)

as '語文',

sum(case department when '數學' then scores else 0 end)

as '數學',

sum(case department when '英語' then scores else 0 end)

as '英語'

from scores

group by student_name

10樓:匿名使用者

select 姓名,

sum(語文) as 語文,

sum(數學) as 數學,

sum(英語) as 英語,

sum(化學) as 化學

from (select 表.姓名,

表.成績 as 語文,

null as 數學,

null as 英語,

null as 化學

from 表

where 表.科目 = '語文'

union all

select 表.姓名,

null as 語文,

表.成績 as 數學,

null as 英語,

null as 化學

from 表

where 表.科目 = '數學'

union all

select 表.姓名,

null as 語文,

null as 數學,

表.成績 as 英語,

null as 化學

from 表

where 表.科目 = '英語'

union all

select 表.姓名,

null as 語文,

null as 數學,

null as 英語,

表.成績 as 化學

from 表

where 表.科目 = '化學')

group by 姓名

這是土辦法,如果你還有其他科目,就再增加 union all ....,

sql語句求大神幫助,SQL查詢語句,求大神幫助!

大概明白你的意思。如果你的資料只有三層,並且只是你sql限定的這些資料好辦。select t3.mc,t2.mc t1.mc,t1.from select from sys zdinfo where plsh 2012070900000068 and plsh 2012070900000072 t1...

求SQL語句,求一個SQL語句

select from table name where 部門 工程部 and 經理簽字 0 and 行政簽字 0 and 結單 0 union all select from table name where 部門 工程部 and 經理簽字 1 and 行政簽字 0 and 結單 0 select...

求一個sql語句。求一個sql寫法

設 表1為mytable1 sl 分類求和後為sl1表2為mytable2 sl 分類求和後為sl2select from select lx sum sl sl1 from mytable1 group by lx a join select lx sum sl sl2 from mytable2...