如何在此 sql 查询中使用 0-20 之间的范围?

How I can use a range between 0-20 on this sql query?

如何在此 sql 查询中使用 0-20 之间的范围?

  $sql = 'SELECT * FROM t1
          LEFT JOIN t2 ON t2.wid = t1.wid  
          UNION
          SELECT * FROM t1
          RIGHT JOIN t2 ON t2.wid = t1.wid
          limit 0, 20';
  $result = db_query($sql);//->fetchField();

将 LIMIT 传递到您的查询中。而且你的限制每次都是可变的,比如

$sql = 'SELECT * FROM (SELECT * FROM t1
          LEFT JOIN t2 ON t2.wid = t1.wid  
          UNION
          SELECT * FROM t1
          RIGHT JOIN t2 ON t2.wid = t1.wid) AS A LIMIT'.$start.','.$end

请清理您的查询,因为这很容易 SQL 注入。对于 drupal,它应该是:

$result = db_query('SELECT * FROM (SELECT * FROM t1
      LEFT JOIN t2 ON t2.wid = t1.wid  
      UNION
      SELECT * FROM t1
      RIGHT JOIN t2 ON t2.wid = t1.wid) AS A LIMIT :min, :max',
      array('min' => $min, 'max' => $max));

有更好的 Drupal 方法来编写此代码,但只要您将变量作为参数传递就可以。