Select a portion from a MySQL Blob Field

前端 未结 2 968
Happy的楠姐
Happy的楠姐 2020-12-28 19:12

I have a table containing lots of data and one of them is a blob. I some times needs to look into this blob for data using PHP.

I do:

select `desc` f         


        
相关标签:
2条回答
  • 2020-12-28 19:58

    This query:

    SELECT 
      CONCAT(
        '...', 
        SUBSTR(`description`, 
          LOCATE('Nam rhoncus', `description`) - 10, 
          (LENGTH('Nam rhoncus') + 20)), 
        '...') AS `description`
    FROM table 
    WHERE `description` LIKE '%Nam rhoncus%';
    

    (I broke it down like this so it's easier to read)

    this will output:

    ...m auctor. Nam rhoncus, purus eg...

    So in your PHP you can do:

    <?php
    define('CHAR_LEFT', 10);
    define('CHAR_RIGHT', 10);
    // db stuff
    $search = mysql_real_escape_string($search_var);
    $query = "SELECT CONCAT('...', SUBSTR(`description`, LOCATE('" . $search . "', `description`) - " . CHAR_LEFT . ", (LENGTH('" . $search . "') + " . (CHAR_LEFT + CHAR_RIGHT) . ")), '...') AS `description` FROM table WHERE `description` LIKE '%" . $search . "%';";
    // then your request
    

    NOTE: Ill be careful using mysql reversed words, this is why I use description instead.

    0 讨论(0)
  • 2020-12-28 20:00

    Something like the following should do what you need:

    SELECT
        SUBSTR(description, INSTR(description, 'Nam rhoncus'), LENGTH('Nam rhoncus')) matchStr
    FROM
        testTable
    WHERE
        description like '%Nam rhoncus%'
    ;
    
    0 讨论(0)
提交回复
热议问题