標題:

PHP 字串分割查詢

發問:

如題 有一資料表A,將一段字串分割取前2個數EX:123456 取12,再將分割好的值用來查詢資料表B...請問要如呵做對應呢? 資料表A | tag | ------------------ | 123456 | 資料表B | no | name | --------------------------- | 12 | 中文 | 將TAG值分割取12,然後再對照到資料表B的no,在顯示出"中文"...請各位高手當忙一下 更新: 感謝Lin的回答,字串分割已經瞭解了,但要把分割好的字串做對應還是不了解,是否請Lin再幫我解答呢? 以下是我的程式碼 更新 2: $link = mysql_connect('localhost', 'root', '123'); mysql_select_db('test'); mysql_query("SET NAMES utf8"); $result = mysql_query("SELECT * FROM `資料表A`"); while($data = mysql_fetch_array($result)){ echo substr($data[0],0,2)." "; } 更新 3: $Correspond = mysql_query("SELECT * FROM `資料表B` "); while($data = mysql_fetch_array($Correspond)) { //echo $data2[1]." "; echo $data[0]." "; echo $data[1]." "; }

最佳解答:

aa.jpg

 

此文章來自奇摩知識+如有不便請留言告知

可使用 substr --- 取得部份字串 以下有詳細說明: http://www.php5.idv.tw/modules.php?mod=books&act=show&shid=637

其他解答:

$str="select a from data2 where d=(select substr(b,1,2) from data where substr(b,1,2)=data2.d)"; //a=要顯示的中文 d=12 substr(b,1,2)裡的b=123456 $str="select a from data2 where d=(select substr(b,1,2) from data where substr(b,1,2)=12)"; 2012-03-16 19:28:55 補充: 第一個會抓全部一樣的 例如 d欄位有12,36,15.. 而b欄位有123456 361235 152369 就會秀出三個中文 第二個只抓是12的 寫的長了點 可能有辦法縮短 2012-03-16 19:30:52 補充: //a=要顯示的中文 d=12 substr(b,1,2)裡的b=123456428DFA428D9FA6F8
arrow
arrow

    fksnlix 發表在 痞客邦 留言(0) 人氣()