作者kgfu (好想睡阿!)
看板PHP
標題[請益] Mysql多資料表查詢
時間Thu Oct 22 16:44:00 2009
問題如下
有四個表格,其欄位數量與欄位名稱都相同
假如我要從第一個表格中,隨機找出一筆資料,並且不與另外三個表格內資料重複
請問該如何做到ˊˋ
目前我的作法是先從表格一隨機找出一筆資料
SELECT * FROM `students` order by rand() limit 1
再利用這筆資料去與其他三個表格搜尋
如下
SELECT * FROM awardlist1 as t1, awardlist2 as t2, awardlist3 as t3 where
t1.student_id=t2.student_id and t2.student_id=t3.student_id and
t1.student_id='X9914003'
回傳卻是為空,改成
SELECT * FROM awardlist1 as t1, awardlist2 as t2, awardlist3 as t3 where
t3.student_id='X9914003' or t2.student_id='X9914003' or
t1.student_id='X9914003'
回傳也是空,明明我再其中一個資料表內已經放入X9914003這筆資料了,還是找不到ˊˋ
請大大教導一下>"<
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.118.155.186
推 appleboy46:用 like '%.....' 10/22 17:12
→ tanxdx:我用了like回傳也是空的耶,怎會這樣,誰可以解答一下 10/22 21:23
→ mathtku:你在"其中"一個資料表 放入資料 那你第一種下法 怎麼會 10/22 21:37
→ mathtku:找的到 因為另外兩個表 根本沒有此筆資料 當然不相等 10/22 21:37
→ tanxdx:我想找這3個資料表的一個欄位有a開頭的文字,我用like 'a%' 10/22 21:43
→ tanxdx:我3個資料表都是同一種欄位,用了原PO大的方法,可是印不出 10/22 21:44
→ kgfu:那請問m大 我該使用怎樣的下法呢??? 10/22 22:57
推 JoeHorn:左轉 Database 板吧...這已經是純 SQL 的問題了... 10/22 23:08
→ maytedm:應該是要用join吧? 10/23 11:20