Re: [SQL ] MYSQL如何組合唯一化?

看板Database作者 (迷惘)時間17年前 (2008/05/22 23:12), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《zeSil (寂靜西風)》之銘言: : 使用版本 MySQL 5.0.51 : table1 : id group1 name1 type1 : 1 A aaa Write : 2 A bbb Report : 3 A ccc Read : 4 B ddd Write : 5 B eee Read : 6 B fff Report : 7 C ggg Write : 將他們組合出來看他們組合類型是如何 : 我的SQL (中略) : 類似這樣的結果 : 可是我想要的是 : Type1 Type2 : Write Report : Write Read : Report Read : 意思是 (Write, Report) = (Report, Write) : 這樣MySQL可以辦到嗎@@? 1、欄位裡如果是有限種類的列舉值,最好不要用一串文字, 可以改成用數字代替。例如Read=1, Write=2, Report=3 (至於數字代表的單字,通常是另開一張Table去存放,有需要再Join) 2、要達到你說的目的,只要在where條件裡多一項a.Type2<b.Type2即可。 (用大於也行) 3、如果不想改Table,可以投機取巧用Length(a.Type2)<Length(b.Type2), 因為剛好Write, Read, Report字數不一樣 4、另外,如果再考慮有可能「等於」的情況,就改成 where a.Type2<b.Type2 or (a.Type2=b.Type2 and a.id<b.id) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.166.140.225 ※ 編輯: diamondking 來自: 118.166.140.225 (05/22 23:16) ※ 編輯: diamondking 來自: 118.166.140.225 (05/22 23:22)
文章代碼(AID): #18DOrckz (Database)
文章代碼(AID): #18DOrckz (Database)