doufu1950 2018-11-02 08:05
浏览 6

如何在学说中使用左子句?

this is my query for find all records in table and sort from the oldest. I want to limit one column to only 50 characters. I can't find any information how to change this query for use left clause.

    $posts = $ChocoPosts->findBy(array(), array('id' => 'DESC'));

Additionally i tried use clean sql syntax for that, but it's not show any records.

 $conn = $this->getDoctrine()->getManager()->getConnection();
$sql = '
    SELECT id, title, left(content, 45), date_add, img
    FROM choco_posts
    ORDER BY id DESC
    ';
$stmt = $conn->prepare($sql);
$stmt->execute();

$stmt->fetchAll();

I'm using symfony4 ant twig template. I display all records in twig for loop.

  • 写回答

2条回答 默认 最新

  • duanfengshang1088 2018-11-02 08:40
    关注

    You can simply use SUBSTRING statement, as example:

    $sql = '
        SELECT id, title, SUBSTRING(content, 1, 45), date_add, img
        FROM choco_posts
        ORDER BY id DESC
        ';
    

    By itself there is little difference, however left can potentially utilize an index while substring cannot.

    Hope this help

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度