这种复杂的sql如何套用discuz自带的方法

ziluopao 发布于 2016/04/27 16:48
阅读 272
收藏 1
PHP

SELECT a.tid, b.attachment FROM pjh_forum_thread AS a, pjh_forum_threadimage AS b WHERE a.attachment IS NOT NULL AND a.tid = a.fid

因为这句中有给表起了个别名,但是查了一下DZ自带的方法,似乎没有这种写法

比如pjh_forum_thread可写成DB::table('forum_thread');但是pjh_forum_thread AS a怎么写呢?

加载中
0
ziluopao
ziluopao

我按这样写为啥不行呢

<?php
    $id = 1;
   $data = DB::fetch_first("SELECT * FROM %t WHERE id=%d",array(
       'test_db',$id
   ));
   debug($data);
?>

<?php
    $id = '2dfsf';
   $data = DB::fetch_first("SELECT * FROM %t WHERE id=%d",array(
       'test_db',$id
   ));
   debug($data);


    $data = DB::fetch_all("SELECT * FROM %t WHERE id > %d AND id <= %d",array(
        'test_db',1,4
    ));
debug($data);

-----------------------------------------------------------------------------

$data = DB::fetch_all("SELECT * FROM %t WHERE name=%s" ,array(
        'test_db',ppc
        ));
    debug($data);

-------------------------------------------------------------------------

$data = DB::fetch_all("SELECT * FROM %t WHERE id IN (%n)" ,array(
        'test_db',array(1,3,4)
        ));
    debug($data);

-------------------------------------------------------------------------

 $query = DB::query("SELECT * FROM %t WHERE id IN (%n)",array(
        'test_db',array(1,2,3)
    ));
    while ($res = DB::fetch($query)){
        $result[] = $res;
    };  
    debug($result);

-----------------------------------------------------------------

//fetch_all和query的区别是fetch_all在内部封装了一个while循环!

$query = DB::query("SELECT * FROM %t WHERE id < %d",array(
        'test_db',4
    ));
    while ($res = DB::fetch($query)){
        $result[] = $res;
    };  
    debug($result);

0
ziluopao
ziluopao
SELECT a.`subject`, a.tid, b.attachment FROM pjh_forum_thread AS a INNER JOIN pjh_forum_threadimage AS b ON a.tid = b.tid WHERE a.attachment IS NOT NULL
返回顶部
顶部