SQL ERROR [ mysqli ] Can't reopen table แก้ยังไงได้บ้างค่ะ

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: SQL ERROR [ mysqli ] Can't reopen table แก้ยังไงได้บ้างค่ะ

Re: SQL ERROR [ mysqli ] Can't reopen table แก้ยังไงได้บ้างค่ะ

โดย thatsawan » 08/09/2016 4:40 pm

mindphp เขียน:อย่า SELECT * FROM tmp_topic_id_new
ใน IN ต้องการ field ไหนก็ระบุไปว่า จะเอา fields ไหน
แก้ไขเเล้วค่ะ เเต่ว่า error มันมาจาก
เราใช้ AND tt.topic_id IN (SELECT (ชื่อฟิลด์) FROM tmp_topic_id_new)) ในคำสั่งเดียว 2 รอบ 3 รอบ ตอนที่ AND

Re: SQL ERROR [ mysqli ] Can't reopen table แก้ยังไงได้บ้างค่ะ

โดย mindphp » 08/09/2016 4:12 pm

อย่า SELECT * FROM tmp_topic_id_new
ใน IN ต้องการ field ไหนก็ระบุไปว่า จะเอา fields ไหน

Re: SQL ERROR [ mysqli ] Can't reopen table แก้ยังไงได้บ้างค่ะ

โดย thatsawan » 08/09/2016 4:10 pm

รู้สาเหตุเเล้วค่ะ
เกิดจาก
WHERE t.topic_id IN (SELECT DISTINCT tt.topic_id FROM (phpbb_m_topictags tt) WHERE tt.topic_id IN (SELECT tt.topic_id FROM (phpbb_m_topictags tt) WHERE tt.tag_id = 442 AND tt.topic_id IN (SELECT * FROM tmp_topic_id_new))

AND tt.topic_id IN (SELECT tt.topic_id FROM (phpbb_m_topictags tt) WHERE tt.tag_id = 440 AND tt.topic_id IN (SELECT * FROM tmp_topic_id_new))

เลือกเกิน 1 ครั้งไม่ได้

โค้ด: เลือกทั้งหมด

SELECT t.topic_id,t.topic_title,t.topic_poster,t.topic_time,t.topic_views,t.topic_first_post_id,t.topic_first_poster_name,t.topic_first_poster_colour,t.topic_last_post_id,t.topic_last_poster_id,t.topic_posts_approved,t.topic_posts_unapproved,t.topic_posts_softdeleted,t.topic_status,t.topic_type,t.topic_last_post_subject,t.topic_last_poster_colour,t.poll_start,t.topic_last_poster_name,t.topic_last_post_time,t.topic_last_view_time,t.forum_id FROM (phpbb_topics t) WHERE t.topic_id IN (SELECT DISTINCT tt.topic_id FROM (phpbb_m_topictags tt) WHERE tt.topic_id IN (SELECT tt.topic_id FROM (phpbb_m_topictags tt) WHERE tt.tag_id = 442 AND tt.topic_id IN (SELECT * FROM tmp_topic_id_new)) AND tt.topic_id IN (SELECT tt.topic_id FROM (phpbb_m_topictags tt) WHERE tt.tag_id = 440 AND tt.topic_id IN (SELECT * FROM tmp_topic_id_new))) AND t.topic_posts_unapproved != 1 ORDER BY t.topic_last_post_time DESC LIMIT 25

Re: SQL ERROR [ mysqli ] Can't reopen table แก้ยังไงได้บ้างค่ะ

โดย thatsawan » 08/09/2016 3:52 pm

sql-error เขียน:ขอคำอธิบาย รายละเอียดเพิ่มเติมประกอบคำถามหน่อยครับ
select topic id ชุดเเรก ที่ได้จากตอนกดค้นหาครั้งเเรก

โค้ด: เลือกทั้งหมด

$sql_array = array(
                'SELECT' => 'DISTINCT tt.topic_id',
                'FROM' => array(
                    $this->m_topictags => 'tt'
                ),
                'WHERE' => array(),
                'LEFT_JOIN' => array(),
                'ORDER_BY' => array(),
            );

            if ($a_search[0]) {
                $sql_array['WHERE'] = 'tt.topic_id IN (' . $a_search[0] . ')';
                $topic_id_search_tmp = $this->db->sql_build_query('SELECT', $sql_array);
            }
ต้องการเก็บข้อมูล topic_id เข้าตาราง TEMPORARY TABLE ถ้ามีข้อมูลชุดเเรกเเล้ว จะได้เอาไปใช้ในการหา เงื่อนไขถัดไป

โค้ด: เลือกทั้งหมด

  $sql = 'CREATE TEMPORARY TABLE tmp_topic_id_new ( topic_id_tmp INT NOT NULL );';
                $result = $this->db->sql_query($sql);
                $sql = 'INSERT INTO tmp_topic_id_new (topic_id_tmp)' . $topic_id_search_tmp;
                $result = $this->db->sql_query($sql);
 
จากนั้น เขียนคำสั่ง SQL มาเพื่อหาเอามาต่อ เงื่อนไขในการหาค่าตัวถัดไป

โค้ด: เลือกทั้งหมด

  $sql_tmp_select = 'SELECT * FROM tmp_topic_id_new';
                $and_tmpsearch = ' AND  tt.topic_id IN (' . $sql_tmp_select . ')';
                foreach ($a_search as $value) {
                    $a_search_and_tmp[] = ' tt.topic_id IN (' . $value . $and_tmpsearch . ')';
                } 

โค้ด: เลือกทั้งหมด

 $sql_array['WHERE'] = implode(' AND', $a_search_and_tmp);
            $topic_id_search = $this->db->sql_build_query('SELECT', $sql_array); 
มันก็จะได้คำสั่ง SQL ชุดนี้

โค้ด: เลือกทั้งหมด

SELECT t.topic_id,t.topic_title,t.topic_poster,t.topic_time,t.topic_views,t.topic_first_post_id,t.topic_first_poster_name,t.topic_first_poster_colour,t.topic_last_post_id,t.topic_last_poster_id,t.topic_posts_approved,t.topic_posts_unapproved,t.topic_posts_softdeleted,t.topic_status,t.topic_type,t.topic_last_post_subject,t.topic_last_poster_colour,t.poll_start,t.topic_last_poster_name,t.topic_last_post_time,t.topic_last_view_time,t.forum_id FROM (phpbb_topics t) WHERE t.topic_id IN (SELECT DISTINCT tt.topic_id FROM (phpbb_m_topictags tt) WHERE tt.topic_id IN (SELECT tt.topic_id FROM (phpbb_m_topictags tt) WHERE tt.tag_id = 442 AND tt.topic_id IN (SELECT * FROM tmp_topic_id_new)) AND tt.topic_id IN (SELECT tt.topic_id FROM (phpbb_m_topictags tt) WHERE tt.tag_id = 440 AND tt.topic_id IN (SELECT * FROM tmp_topic_id_new))) AND t.topic_posts_unapproved != 1 ORDER BY t.topic_last_post_time DESC LIMIT 25

เเต่ผลคือมัน Can't reopen table: 'tmp_topic_id_new'

Re: SQL ERROR [ mysqli ] Can't reopen table แก้ยังไงได้บ้างค่ะ

โดย thatsawan » 08/09/2016 3:38 pm

ลองเป็น ตารางธรรมดามันไม่ error ค่ะ

โค้ด: เลือกทั้งหมด

SELECT t.topic_id,t.topic_title,t.topic_poster,t.topic_time,t.topic_views,t.topic_first_post_id,t.topic_first_poster_name,t.topic_first_poster_colour,t.topic_last_post_id,t.topic_last_poster_id,t.topic_posts_approved,t.topic_posts_unapproved,t.topic_posts_softdeleted,t.topic_status,t.topic_type,t.topic_last_post_subject,t.topic_last_poster_colour,t.poll_start,t.topic_last_poster_name,t.topic_last_post_time,t.topic_last_view_time,t.forum_id FROM (phpbb_topics t) WHERE t.topic_id IN (SELECT DISTINCT tt.topic_id FROM (phpbb_m_topictags tt) WHERE tt.topic_id IN (SELECT tt.topic_id FROM (phpbb_m_topictags tt) WHERE tt.tag_id = 442 AND tt.topic_id IN (SELECT * FROM tmp_topic_id_new)) AND tt.topic_id IN (SELECT tt.topic_id FROM (phpbb_m_topictags tt) WHERE tt.tag_id = 440 AND tt.topic_id IN (SELECT * FROM tmp_topic_id_new))) AND t.topic_posts_unapproved != 1 ORDER BY t.topic_last_post_time DESC LIMIT 25

Re: SQL ERROR [ mysqli ] Can't reopen table แก้ยังไงได้บ้างค่ะ

โดย sql-error » 08/09/2016 3:37 pm

ขอคำอธิบาย รายละเอียดเพิ่มเติมประกอบคำถามหน่อยครับ

SQL ERROR [ mysqli ] Can't reopen table แก้ยังไงได้บ้างค่ะ

โดย thatsawan » 08/09/2016 3:35 pm

โค้ด: เลือกทั้งหมด

  $sql_array = array(
                'SELECT' => 'DISTINCT tt.topic_id',
                'FROM' => array(
                    $this->m_topictags => 'tt'
                ),
                'WHERE' => array(),
                'LEFT_JOIN' => array(),
                'ORDER_BY' => array(),
            );

            if ($a_search[0]) {
                $sql_array['WHERE'] = 'tt.topic_id IN (' . $a_search[0] . ')';
                $topic_id_search_tmp = $this->db->sql_build_query('SELECT', $sql_array);
            }

            if (count($a_search) != 1) {

                $sql = 'CREATE TEMPORARY TABLE tmp_topic_id_new ( topic_id_tmp INT NOT NULL );';
                $result = $this->db->sql_query($sql);
                $sql = 'INSERT INTO tmp_topic_id_new (topic_id_tmp)' . $topic_id_search_tmp;
                // echo $sql; exit;
                $result = $this->db->sql_query($sql);
               
                $sql_tmp_select = 'SELECT * FROM tmp_topic_id_new';
                // ---- debug ว่ามีเลข topic เข้าตารางหรือยัง 
//                $result = $this->db->sql_query($sql_tmp_select);
//
//                while ($row = $this->db->sql_fetchrow($result)) {
//                    $topic_id_tmp[] = $row['topic_id_tmp'];
//                }
//                print_r($topic_id_tmp);
//                echo '<hr>';
                //---- debug ว่ามีเลข topic เข้าตารางหรือยัง 

                $and_tmpsearch = ' AND  tt.topic_id IN (' . $sql_tmp_select . ')';
                foreach ($a_search as $value) {
                    $a_search_and_tmp[] = ' tt.topic_id IN (' . $value . $and_tmpsearch . ')';
                }
                //   print_r($a_search_and_tmp); echo '<hr>';
                //$a_search[] = 'tt.topic_id IN (' . $where_nametag . ')';
                $sql_array['WHERE'] = implode(' AND', $a_search_and_tmp);
            }


            $topic_id_search = $this->db->sql_build_query('SELECT', $sql_array);

ข้างบน