ผมจะค้นหาแบบข้อความ  เช่น ค้นหา คำว่า hello world



แต่ในฐานข้อมูลผมมี hello    world   ทำให้ค้นหาคำว่า hello world ไม่เจอ



มีวิธีไหนบ้าง ครับ ทำให้ ค้นหา เจอ   ขอบคุณครับ


กรกฎ วิริยะ 21 Feb 2011 17:02
โดยปกติแล้ว เราจะใช้วิธีแยกข้อมูลค้นหาออกเป็นคำๆ ครับ เช่นจากโจทย์ เราจะคนหาคำว่า hello หรือ word ครับ



เวลาค้นหาก็จะได้ประมาณ



WHERE detail LIKE '%hello%' OR detail LIKE '%word%'
t007 xx 21 Feb 2011 17:31
ลองดูตามนี้คับ (มือใหม่คับผม ^ ^)

$string="Hello World";

$words==preg_split("/[\s]+/",$string);#แยกออกทีละคำ โดยแบ่งตามช่องว่าง (" ")

$sql="SELECT * FROM tb_product WHERE pd_id <> '' AND ( ";

foreach($words as $word){

    if($i<count($words)){#ตรวจสอบว่าถึงคำสุดท้ายหรือยัง

    $sql.=" pd_detail LIKE '%$word%' OR ";    

    }else{#ถ้าถึงคำสุดท้ายแล้ว ให้เอาคำสั่ง OR ออก

    $sql.=" pd_detail LIKE '%$word%'";    

    }

    $i++;

}

$sql.=")";
BlueEye วิโรจน์ คชินทร์ 22 Feb 2011 08:04


LINK ======> LIKE


t007 xx 22 Feb 2011 13:11
Quotations by Comment #3


LINK ======> LIKE




โทดทีคับ ^ ^"


quote edit delete

ลองดูตามนี้คับ (มือใหม่คับผม ^ ^)

$string="Hello World";

$words==preg_split("/[\s]+/",$string);#แยกออกทีละคำ โดยแบ่งตามช่องว่าง (" ")

$sql="SELECT * FROM tb_product WHERE pd_id <> '' AND ( ";

foreach($words as $word){

    if($i<count($words)){#ตรวจสอบว่าถึงคำสุดท้ายหรือยัง

    $sql.=" pd_detail LINE '%$word%' OR ";    

    }else{#ถ้าถึงคำสุดท้ายแล้ว ให้เอาคำสั่ง OR ออก

    $sql.=" pd_detail LINE '%$word%'";    

    }

    $i++;

}

$sql.=")";
ty900ty900 22 Feb 2011 13:55


ขอบคุณครับ


BlueEye วิโรจน์ คชินทร์ 22 Feb 2011 15:03


ใจเย็นๆครับท่าน t007 พิมพ์ผิดอีกหรือเปล่านั่น



LINE or LIKE



แล้วก็ตรง if($i<count($words)){  ให้เพิ่ม -1 เป็นแบบนี้ครับ



if($i<count($words)-1){