PHP子串行选择mysql

时间:2013-06-13 23:41:23

标签: php mysql substring

我喜欢从mysql表中将第二行'text2'子串。 谢谢!

    $sql = "select e.id, e.text1, e.text2, e.text3, count(r.id)     reportCount " . 
    "from employee e left join employee r on r.managerId = e.id " .
    "group by e.id order by e.text1, e.text2";

    try { ...

2 个答案:

答案 0 :(得分:0)

在select语句中,您可以使用case when when

select e.id, e.text1, 
    CASE 
       WHEN CHAR_LENGTH(e.text2) >= 30 THEN 
         SUBSTR(e.text2,0,30) 
       ELSE 
         e.text2 
      END as text2,
    ....

我刚写完没有测试

答案 1 :(得分:0)

Gökhan,谢谢你的帮助。 我已经解决了它:

select e.id, e.text1, (SUBSTRING(e.text2,1,30)) AS 'text2' 

或带点:

select e.id, e.text1,
    CASE 
    WHEN CHAR_LENGTH(e.text2) > 30 THEN CONCAT(SUBSTRING(e.text2, 1, 30), '...')
    ELSE e.text2
    END AS text2