iT邦幫忙

0

(已解決)SQL+PHP請教

  • 分享至 

  • twitterImage

table如下:

No Qes User Ans Group
1   1  Amy   5    A 
2   2  Amy   4    A
3   3  Amy   3    A
4   1  May   5    B
5   2  May   3    B
6   3  May   2    B

請問如何使用SQL語法+PHP,
找出上表中Ans欄位不一樣的Qes,
並產生出如下table:

No Group_A Group_B Qes
1    Amy     May    2
2    Amy     May    3

謝謝

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
暐翰
iT邦大師 1 級 ‧ 2018-03-28 16:00:05
最佳解答
--測試資料建立
create table #Tem_Table ([No] int,[Qes] int,[User] nvarchar(20),[Ans] int,[Group] nvarchar(20) );
insert into #Tem_Table ([No],[Qes],[User],[Ans],[Group]) values 
	(1,1,'Amy',5,'A')
	,(2,2,'Amy',4,'A')
	,(3,3,'Amy',3,'A')
	,(4,1,'May',5,'B')
	,(5,2,'May',3,'B')
	,(6,3,'May',2,'B')
;

--組合你要的結果
select 
	ROW_NUMBER() OVER (order by T300.Qes) No,
	T300.Group_A,
	T300.Group_B,
	T300.Qes
from (
	select 
		case 
			when T100.[Group] = 'A' then T100.[User]
			when T200.[Group] = 'A' then T200.[User]
		end
		as Group_A,
		case 
			when T100.[Group] = 'B' then T100.[User]
			when T200.[Group] = 'B' then T200.[User]
		end
		as Group_B,
		T100.Qes Qes,
		ROW_NUMBER() OVER (PARTITION BY T100.Qes order by T100.Qes) [rank]
	from #Tem_Table T100
	inner join #Tem_Table T200 on T100.qes = T200.qes and T100.ans <> T200.ans
) as T300
where T300.[rank] = 1

結果圖:

連結: 回答:SQL+PHP請教(SQL Server版本)


更新MySQL版本:

-- 測試資料建立
drop table Tem_Table;
create table Tem_Table (`No` int,`Qes` int,`User` nvarchar(20),`Ans` int,`Group` nvarchar(20) );
insert into Tem_Table (`No`,`Qes`,`User`,`Ans`,`Group`) values 
	(1,1,'Amy',5,'A')
	,(2,2,'Amy',4,'A')
	,(3,3,'Amy',3,'A')
	,(4,1,'May',5,'B')
	,(5,2,'May',3,'B')
	,(6,3,'May',2,'B')
;


select 
    @rownum:=@rownum + 1 as No,T300.*
from (
	select DISTINCT 
		case 
			when T100.Group = 'A' then T100.User
			when T200.Group = 'A' then T200.User
		end
		as Group_A,
		case 
			when T100.Group = 'B' then T100.User
			when T200.Group = 'B' then T200.User
		end
		as Group_B,
		T100.Qes Qes
	from Tem_Table as T100
	inner join Tem_Table as T200 on T100.qes = T200.qes and T100.ans <> T200.ans
) as T300
,(SELECT @rownum := 0) r;

連結:回答:SQL+PHP請教 (MySQL版本)


原理:


這邊的意思是找出相同題目但是不同答案
使用inner join
原理可以看: INNER JOIN 關鍵字 (SQL INNER JOIN Keyword) - 內部連接



這邊為了組合你的Group_A跟Group_B欄位使用

原理可以看: MySQL CASE Function



distinct是因為使用inner join會產生重複資料,要篩選掉,所以使用

原理可以看: SQL SELECT DISTINCT Statement


使用rownum變數是為了組合出你要的No
一筆資料就+1

原理可以看: MySQL Variables

看更多先前的回應...收起先前的回應...
小斑 iT邦新手 3 級 ‧ 2018-03-28 16:46:07 檢舉

謝謝/images/emoticon/emoticon32.gif
我先貼上您提供的"測試資料建立"語法~
但是無法建立資料?如下圖
https://ithelp.ithome.com.tw/upload/images/20180328/20106496E15NMDZU6r.jpg

然後"組合我要的結果"部份我還在理解中~但也是出現蠻多紅色XX的,請問是不是SQL語法不同的問題?我是用MySQL

暐翰 iT邦大師 1 級 ‧ 2018-03-28 16:58:26 檢舉


連結:回答:SQL+PHP請教
這是SQL Server版本

你是MySQL我忘了,等等我在寫一版給你

小斑 iT邦新手 3 級 ‧ 2018-03-28 17:02:59 檢舉

不好意思是我一開始沒講清楚/images/emoticon/emoticon06.gif
真的太謝謝您了~

暐翰 iT邦大師 1 級 ‧ 2018-03-28 17:21:20 檢舉

我更新MySQL版本在回應裡面了

小斑 iT邦新手 3 級 ‧ 2018-03-28 17:54:51 檢舉

謝謝,可以順利組合成我要的結果
但是我不太懂"組合我要的結果"的語法,可以請教您語法意思嗎?謝謝

暐翰 iT邦大師 1 級 ‧ 2018-03-28 18:08:09 檢舉

我更新原理了
有不懂地方再告訴我

小斑 iT邦新手 3 級 ‧ 2018-03-28 18:22:49 檢舉

謝謝,我想再請教

  1. T200是指什麼
  2. 這句→T100.Qes Qes
  3. 這句→,(SELECT @rownum := 0) r;
暐翰 iT邦大師 1 級 ‧ 2018-03-28 18:25:57 檢舉

T200是指什麼

可以用php變數理解,指的是Temp_table這個表格

T100.Qes Qes

第一個Qes指定T100變數表格的Qes欄位
第二個Qes是更換欄位名稱

(SELECT @rownum := 0) r;

這是宣告一個變數rownum,初始值0
接著用r變數當表格保存

小斑 iT邦新手 3 級 ‧ 2018-03-28 18:34:03 檢舉

謝謝/images/emoticon/emoticon32.gif

我要發表回答

立即登入回答