开发者

mysql retrieve partial column

I am using TinyMCE to allow users to write in what will eventually be submitted to a database.

TinyMCE includes a button which will insert a "pagebreak" (actually just an HTML comment <!--pagebreak-->).

How can I later pull back everything up to the first occurrence of the pagebreak "tag"? There might be a significant bit of data after that tag which I could discard via php later, but it seems like I shouldn't even bring it back. So I can I SELECT just par开发者_运维知识库t of a column if I never know how far in to the data the <!--pagebreak--> will be?

EDIT: The below answer does work, and will get me where I need to go.. I am just curious as to if there is an equally simple way to handle bringing back the whole column if it is short and thus the pagebreak tag doesn't exist. With the below solution, if no pagebreak is found, an empty string is returned.

If not, then I can easily handle it in the code.


Something like this should get you everything up to the <!--:

SELECT SUBSTRING(my_col FROM 1 FOR POSITION('<!--' IN my_col)-1) AS a_chunk 
  FROM my_table

Reference for string functions.

Edit:
Just putting OMG Ponies' words into SQL:

SELECT CASE WHEN position('<!--' IN my_col) > 0 
            THEN SUBSTRING(my_col FROM 1 FOR POSITION('<!--' IN my_col)-1)
            ELSE my_col END AS a_chunk
  FROM my_table

It sounds like you'll also want a check on the length of the text; whether or not there is a page break. You can use CHARACTER_LENGTH() for that.


why not create another column in your mysql table to store integer value of the position where <!--pagebreak--> is. You'll calculate this value in your script and store this value at same time when you insert the html generated by tinymce.

Then in later retrievals use the value in that column to select the substring from 1 up to the value. This should make your query simpler and maybe improve query performance?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜