如何删除mysql中的部分字符串?

时间:2011-09-20 07:09:11

标签: mysql

在我的数据库的一个表中,我有一些看起来像这样的字符串:

sometext-othertext

如何使用SELECT语句删除包含破折号的文本,结果只是sometext

5 个答案:

答案 0 :(得分:23)

在第一次出现分隔符“ - ”之前返回子字符串:

SELECT SUBSTRING_INDEX('foo-bar-bar', '-', 1) as result;

输出结果=“foo”

您可以在获取子字符串

之前将1替换为您想要的出现次数

SELECT SUBSTRING_INDEX('foo-bar-bar', '-', 2) as result;

输出结果=“foo-bar”

参考:http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring-index

答案 1 :(得分:9)

<击>     SELECT REPLACE(“sometext-othertext”,“ - ”,“”)as newvalue

这应该这样做。
编辑:

  SELECT SUBSTRING_INDEX('sometext-othertext', '-', 1) as newvalue;

对于之前不理解这个问题道歉。

答案 2 :(得分:2)

有两种方法可以对此进行故障排除,如果你想显示全部(只是删除&#34; - &#34;符号,我建议使用它,因为速度更快:

SELECT REPLACE('sometext-othertext','-','');

语法改变文本&#34; - &#34;到&#34;&#34; (空文本,因为你只想删除它)

如果你只想显示第一部分(例如:sometext)或只显示第二部分(例如:othertext),你可以使用它来分割字符串:

SELECT SUBSTRING_INDEX('sometext-othertext', '-', 1);

答案 3 :(得分:0)

最好使用mysql REPLACE函数将“ - ”替换为“”

答案 4 :(得分:0)

只需使用:

selected = None

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                if selected:  # If a piece was selected ...
                    selected = None  # Deselect it.
                else:
                    # Find a colliding block.
                    collided_sprites = [block for block in blocks
                                        if block.rect.collidepoint(event.pos)]
                    if collided_sprites:  # If we clicked a block ...
                        # Select the parent of the block.
                        selected = collided_sprites[0].parent
                        # Calculate the offsets for the blocks.
                        for block in selected.blocks[0]:
                            block.offset = block.rect.topleft - Vec(event.pos)
        elif event.type == pygame.MOUSEMOTION:
            if selected:
                # Update the block positions by adding their offsets to the mouse pos.
                for block in selected.blocks[0]:
                    block.rect.topleft = event.pos + block.offset

    screen.fill(BLACK)
    blocks.draw(screen)
    pygame.display.update()
    fpsClock.tick(25)

函数有3个参数,第一个是文本,第二个是拆分对象,数字(1)是你想要的(控制器)